## About string to double !?!

bridged with qdn.public.qnxrtp.applications
Bill Caroselli (Q-TPS)

### Re: About string to double !?!

Well by "to the last bit" I mean this:
for any given 'float f' consider what is the smallest
increment/decrement that can be represented by that constant. I.E. what is
the smallest amount that will generate a different binary value.

Then when we want:
if( float_1 == float_2 )
I'm willing to accept:
if( float_1 + smallest_increment <= float_2 - smallest_increment
&& float_1 - smallest_increment >= float_2 + smallest_increment )
as an acceptable calculation.

Yes, I know that this represents a lot of extra work. But you would only
have to do it when you needed this kind of testing.

"Weijie Zhang" <wzhang@qnx.com> wrote in message
news:ai9dcd\$jm2\$1@nntp.qnx.com...
"Bill Caroselli (Q-TPS)" <QTPS@EarthLink.net> wrote in message
news:ai6jht\$dtf\$1@inn.qnx.com...
Has anyone come up with a math library that will do comparisons down to
the
last bit that CAN be accurately identified?

There is no a "standard" lib for that because it is "embedded" into your
definition of "good" value. For example,
you can use absolute difference or relative difference as the "cut-off"
policy.
As to "go to the last bit" (how do you define?), you can know it is
dangerous
simply by noticing the fact that a school of scientific workers are taking
of its randomness ..., they call that "Monte Carlo" simulation ...
In the practical world, you may consider also a way of
1) first abstract your model into integer field
(simply because you have up to 2^31 value to play with
and the space is hugh enough for me (order of 2^32 elements! may cost
my
whole life to count though:)...)
2) play in integer field instead.
Although there are a lot of c playing in numerical simulation, fortran
sounds to me
more seriously.

Weijie

I.E.
If I really want to test:
if( f == 0.9 )
I wouldn't want to write in my code
if( f > 0.89999 && f < 0.90001 )
if the value of 0.900003 could pass as equal when in fact the correct
value
should be
0.89999999927

Alain Bonnefoy

### Re: About string to double !?!

It should be possible to set a global math precision at the beginnng of
the prog, says FLOAT_SIGNIFICANT_DIGITS = 15, in order that all
comparisons are made up to the 15th decimal digit!

Alain.

Weijie Zhang wrote:
"Bill Caroselli (Q-TPS)" <QTPS@EarthLink.net> wrote in message
news:ai6jht\$dtf\$1@inn.qnx.com...

Has anyone come up with a math library that will do comparisons down to

the

last bit that CAN be accurately identified?

There is no a "standard" lib for that because it is "embedded" into your
definition of "good" value. For example,
you can use absolute difference or relative difference as the "cut-off"
policy.
As to "go to the last bit" (how do you define?), you can know it is
dangerous
simply by noticing the fact that a school of scientific workers are taking
of its randomness ..., they call that "Monte Carlo" simulation ...
In the practical world, you may consider also a way of
1) first abstract your model into integer field
(simply because you have up to 2^31 value to play with
and the space is hugh enough for me (order of 2^32 elements! may cost my
whole life to count though:)...)
2) play in integer field instead.
Although there are a lot of c playing in numerical simulation, fortran
sounds to me
more seriously.

Weijie

I.E.
If I really want to test:
if( f == 0.9 )
I wouldn't want to write in my code
if( f > 0.89999 && f < 0.90001 )
if the value of 0.900003 could pass as equal when in fact the correct

value

should be
0.89999999927

David Gibbs

### Re: About string to double !?!

Well by "to the last bit" I mean this:
for any given 'float f' consider what is the smallest
increment/decrement that can be represented by that constant. I.E. what is
the smallest amount that will generate a different binary value.

Then when we want:
if( float_1 == float_2 )
I'm willing to accept:
if( float_1 + smallest_increment <= float_2 - smallest_increment
&& float_1 - smallest_increment >= float_2 + smallest_increment )
as an acceptable calculation.
There are a couple pieces of fuzziness with this thinking. First,
floating points are represented by mantissa & exponent. So defining
the "smallest increment" is pretty tricky, as it won't be a simple
number -- you could look at smallest increment of mantissa, though.
(But as a REAL number, the smallest increment might be .5, might
be .000005, might be far smaller, or might be larger, depending on the
exponent.)

It sounds like you're trying to say that f1 == f2 if they differ by
less than the smallest increment -- but that is exactly what does
happen. Of course, your particular test is written incorrectly,
consider if f1 & f2 in your test were integers. The smallest possible
increment for an integer is 1.

So, your saying that f1=f2 if:
(f1 + 1) <= (f2 -1) && (f1-1 >= f2 +1 )
Let's try that with 5... 5=5 if:
(5+1) <= (5-1) && (5-1) >= (5+1)
if
(6 <=4) && (4 >= 6)

Oops.

But, the problem isn't with this -- the problem is with expecting that
0.9 can be exactly represented in float, when it can't.

People will do some calculations, and expect that the answer for
the calculations, which if done in REAL number would exactly equal
0,9 will also exactly equal the best approximation to 0,9 that can
be represented -- but what usually happens is the approximation of
the result and the approximation of 0.9 will be slightly different.

-David
--
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs

### Re: About string to double !?!

Alain Bonnefoy <alain.bonnefoy@icbt.com> wrote:
It should be possible to set a global math precision at the beginnng of
the prog, says FLOAT_SIGNIFICANT_DIGITS = 15, in order that all
comparisons are made up to the 15th decimal digit!
Neat thought.

Of course... the compiler would have to be modified -- right now
(f1 == f2) just does a straight bit-wise comparison. (At least
in C). You'd have to get into operator overloading & stuff like
that... though in C++ you could do this, define your own floating
point type, define your comparison operator, and have it look at
a global significant digits flag in order to know how many significant
digits to use for comparison.

Hm... prototypes for math libraries & calls to them might be a bit
messy, but could be worked around.

Of course, you also have to consider whether you mean binary or
decimal digits. 15 decimal digits would require about 50 binary
digits.

-David
--
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.