Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Comparing double in for loop

Reply
Thread Tools

Comparing double in for loop

 
 
Nick Keighley
Guest
Posts: n/a
 
      04-10-2008
On 9 Apr, 14:26, (E-Mail Removed) wrote:


> i have encountered the strangest behavior. Check out this simple
> program:
>
> #include <stdio.h>
>
> int main()
> {
> * double time = 1;
> * double i = 0;
>
> * for (double i = 0; i < time ; i+=0.01 )
> * {
> * * if ( i == 0.15 *)
> * * {
> * * * printf( "found %f\n", i);
> * * }
> * * if ( i < 0.1 )


here you test if i < 0.1. Assuming the first
test fails (it is very likely to) this is
the only print statement in your program


> * * {
> * * * printf( "foundXX %f\n", i);
> * * }
> * }
>
> * return 1;
>
> }
>
> What would you expect to be printed:
> All the numbers from 0.0 to 0.9 with the prefex: foundXX


um. Why would you expect anything larger than 0.1 to be
printed?


> and last line in output should be found 0.15 - right?!


no

> Wrong... what I get is all the numbers from 0.0 to 0.1 printed
> (including 0.1!!)


kool. So what is the problem

> When checking if ( i==0.1) { printf( "foundXX %f\n",i);} it does not
> print foundXX 0.1!!
> Why exactly does it think that 0.1 is < than 0.1!!??


so other responses


--
Nick Keighley

 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-10-2008
Juha Nieminen wrote:
> Ioannis Vranos wrote:
>> Then why doesn't an implementation use a large number of bits, for
>> people that want accuracy and also for x==y to work?

>
> Sure, if you want your program to be a thousand times slower.
>
> The C++ compiler will usually use the FPU (and sometimes even SSE) to
> make floating point calculations in hardware. To get larger floating
> point values you would have to use a software library, which would be
> enormously slower.
>
> Besides, 0.1 is inaccurate in binary floating point format regardless
> of the number of bits used (for the exact same reason as 1/3 is
> inaccurate in decimal format regardless of how many decimals you use).



OK, but in math we have a symbol to represent the infinite repeating
sequences in decimals, like 7.33333... or 7.343434...
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      04-10-2008
Ioannis Vranos wrote:
> OK, but in math we have a symbol to represent the infinite repeating
> sequences in decimals, like 7.33333... or 7.343434...


Floating point numbers are not the set of real numbers, nor are they
symbolic numbers. They are binary numbers with a fixed amount of bits.
You can't expect to be able to do with them what you can do in
"mathematics". You can only expect being able to approximate a finite
amount of operations.
 
Reply With Quote
 
brian tyler
Guest
Posts: n/a
 
      04-10-2008
> A large number of bits is not enough, you'd need an *inifinte* number
> of bits. Even if you extend the "normal" double numbers concept by
> fractions (e.g. store two integer numbers 1 and 10 to represent 1/10),
> you cannot represent the whole rational set (e.g. sqrt(2), pi or e
> cannot be stored like this without loss of precision). Also, there is
> an issue in performance: the more memory you use to store a single
> value, the longer it will take to operate on them.


/pedant mode on

sqrt(2), pi and e are not in "the rational set" they are irrational,
meaning that they cannot be written as the quotient of two integers.
Moreover pi and e are transcendental meaning that they cannot be
expressed as the root of a polynomial equation (like sqrt(2) can since
it is one root of X^2 - 2 = 0). What you mean is the real set.

/pedant mode off

 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-10-2008
Juha Nieminen wrote:
> Ioannis Vranos wrote:
>> OK, but in math we have a symbol to represent the infinite repeating
>> sequences in decimals, like 7.33333... or 7.343434...

>
> Floating point numbers are not the set of real numbers, nor are they
> symbolic numbers. They are binary numbers with a fixed amount of bits.
> You can't expect to be able to do with them what you can do in
> "mathematics". You can only expect being able to approximate a finite
> amount of operations.



Well I could think of an implementation where the (last) repeating
sequences could be identified with the use of extra bits. For example an
additional byte could be reserved and used for this in the style:

10000000 = The last decimal digit is repeating indefinetely,
example: 1.1111111111111111....

11000000 = The two last decimal digits are repeating indefinitely,
example: 1.1212121212121212....

11100000 = The three last decimal digits are repeating indefinitely,
example: 2.233234123123123123....

11110000 = The four last decimal digits are repeating indefinitely,
example, 1.543424321432143214321.....

etc.


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-10-2008
Ioannis Vranos wrote:
> Juha Nieminen wrote:
>> Ioannis Vranos wrote:
>>> OK, but in math we have a symbol to represent the infinite repeating
>>> sequences in decimals, like 7.33333... or 7.343434...

>> Floating point numbers are not the set of real numbers, nor are they
>> symbolic numbers. They are binary numbers with a fixed amount of bits.
>> You can't expect to be able to do with them what you can do in
>> "mathematics". You can only expect being able to approximate a finite
>> amount of operations.

>
>
> Well I could think of an implementation where the (last) repeating
> sequences could be identified with the use of extra bits. For example an
> additional byte could be reserved and used for this in the style:
>
> 10000000 = The last decimal digit is repeating indefinetely,
> example: 1.1111111111111111....
>
> 11000000 = The two last decimal digits are repeating indefinitely,
> example: 1.1212121212121212....
>
> 11100000 = The three last decimal digits are repeating indefinitely,
> example: 2.233234123123123123....
>
> 11110000 = The four last decimal digits are repeating indefinitely,
> example, 1.543424321432143214321.....
>
> etc.



More accurately an example of storing the value of the second example
would be:

Stored in double bits:

1.12


Repeating flags (in our case one byte reserved for this):

11000000 = means .12 is repeated indefinitely
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-10-2008
Ioannis Vranos wrote:
> Juha Nieminen wrote:
> Well I could think of an implementation where the (last) repeating
> sequences could be identified with the use of extra bits. For example an
> additional byte could be reserved and used for this in the style:


How about simply using integers, as I suggested in my other post?
Much easier and very efficient.
 
Reply With Quote
 
Michael.Boehnisch@gmail.com
Guest
Posts: n/a
 
      04-10-2008
On 10 Apr., 15:48, brian tyler <(E-Mail Removed)> wrote:
> /pedant mode on
> What you mean is the real set.
> /pedant mode off


I was only testing if you all pay attention. Honest. No, really. Could
these eyes lie???

You got me here.

Michael


 
Reply With Quote
 
Michael.Boehnisch@gmail.com
Guest
Posts: n/a
 
      04-10-2008
On 10 Apr., 16:01, Ioannis Vranos <(E-Mail Removed)>
wrote:
> Juha Nieminen wrote:
> Well I could think of an implementation where the (last) repeating
> sequences could be identified with the use of extra bits. For example an
> additional byte could be reserved and used for this in the style:


By adding some extra bits, you certainly can extend the set of
representable real numbers. However, the length of the periodically
repeated portion also can have arbitrary length (up to the size of
the denominator if you convert to a normalized fraction). A number
like 1/x where x is roughly 10^9 may have a worst case period length
of 10^9... (of course, not all numbers in this range are *so* nasty).
You'd need to store this sequence somewhere at least once.

You'd be better off by just adding the extra bits to the mantissa.

best,

Michael
 
Reply With Quote
 
Brian Tyler
Guest
Posts: n/a
 
      04-11-2008
On Thu, 10 Apr 2008 07:40:06 -0700, Michael.Boehnisch wrote:

> On 10 Apr., 15:48, brian tyler <(E-Mail Removed)> wrote:
>> /pedant mode on
>> What you mean is the real set.
>> /pedant mode off

>
> I was only testing if you all pay attention. Honest. No, really. Could
> these eyes lie???
>
> You got me here.
>
> Michael


Haven't used the pedant mode tags in a while

Brian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
comparing field data from previous field in loop with null values abbylee26@hotmail.com ASP General 6 02-15-2006 01:59 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Wrong results when comparing negative double variables in an if statement John C Programming 11 05-05-2004 04:06 PM
Problem comparing a double with 0 =?iso-8859-1?Q?Juli=E1n?= Albo C++ 2 07-02-2003 06:05 PM



Advertisments