Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- PC is IEEE754?
- 10-18-2008
Re: PC is IEEE754?
As Jerry said, you can find out for yourself whether this prints 1 or not.
However, I would like to point out that IEEE-754 compliance does NOT
guarantee the result. 19.6 cannot be represented exactly in binary. In
binary, it is an infinitely repeating value. Thus, the floating point
representation will be an approximation -- very close to 19.6, but not
exactly equal.
When you multiply by 100, it is quite difficult (without knowing the exact
binary representation) to predict whether rounding will help or hurt.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Re: PC is IEEE754?
Begs the question: Why multiply by 100? Is 19.6 perhaps a $.cents amount? If
so then don't. Store and perform calculations on monetory amounts in
cents(which are always integers and integers are stored exactly in floating
point) and only convert (with suitable rounding) to $s on output.
Re: PC is IEEE754?
Because this here IRS form or physics book of formulas says to. If
the number is 100, it probably involves either percentages or metric
units conversion.
Cents are *NOT* always integer amounts (ever calculated sales tax?
bought gasoline? bought or sold penny stocks?). Sometimes you
have to add up a bunch of fractional cents, *THEN* round. And
sometimes the law gets ridiculously complicated stipulating *HOW*
to round in ways that violate mathematical convention. For example,
in New York State in the late 1970's, you rounded sales tax on 0.17
differently from $12.17 which was rounded differently from $2.17
when calculating sales tax (they seemed to want to avoid having
very small sales escape any tax at all.). Another formula had you
always round up any fraction greater than $0.0025 to the next higher
cent.
The general advice to perform addition/subtraction on monetary
amounts using an integer quantity of the smallest unit of currency
is a good one, provided that it can't overflow. A 32-bit signed
integer (C guarantees an integer at least this large) quantity of
cents only goes to a bit over $21.4 million. That might not be
enough for a baseball player adding up his lifetime income, or even
5-year income averaging. You also might have to carry more precision
briefly before rounding. The appropriate unit isn't necessarily
cents even if the currency is US dollars.
A 64-bit signed integer (C89 does not guarantee an integer type
this large) should be large enough for all but the largest governments
and international banks.
An integer quantity in a IEEE double gets you a bit more range (53
bits) before the integers start becoming inexact. This might be a
better strategy than long long if you need to be portable to
a system without long long.
Site Timeline
- » Pretty Good Free PHP and MySQL Development Site
- — Next thread in » PHP Scripting Forum
- » calling MySQL stored function using mysql_
- — Previous thread in » PHP Scripting Forum
- » URL redirection
- — Newest thread in » PHP Scripting Forum
- » SSD partition alignment revisited
- — The site's Newest Thread. Posted in » Computer Hardware