NO

Author Topic: double representation  (Read 9823 times)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1758

aardvajk

  • Guest
Re: double representation
« Reply #16 on: March 01, 2012, 12:35:50 am »
If you want to know what the Pelles CRT is doing in this situation, its source code is ostensibly the same as the code at ftp://sccn.ucsd.edu/pub/DataSuite/tmp/bds/Install/program%20files/Borland/BDS/4.0/source/cpprtl/Source/dinkumware/source/
« Last Edit: March 01, 2012, 12:38:15 am by aardvajk »

CommonTater

  • Guest
Re: double representation
« Reply #17 on: March 01, 2012, 01:08:00 am »
Or is that "last bit ambiguity" caused by something other?

It's a result of rounding errors in the FPU *hardware* ... with a value such as 22/7 ... there is no exact binary representation so the  least significant bit is not predictable... it might be a 1 or a 0 ... It might vary from compiler to compiler and it might vary from FPU to FPU ... (eg.  Intel and AMD might give different answers as might Pelles and MinGW).

With float values things like 16/2 could give you 8, 7.99999999, 8.00000001 for example.  Doubles are better because of their higher bit resolutions, but the same issue is still there. 

Go back to my little test program and run it...
Quote
// demonstration of floating point inaccuracies
#include <math.h>
#include <stdio.h>

int main (void)
  { int i;
    float x = 1.0;

    for (i = 0; i < 100; i++)
      { printf("%f\t",x);
        x += 0.1; }
    return 0; }

... change the float to a double and you should see that it gives you correct answers up to 10 But these are very small values... as the numbers get bigger or have more decimal points the more likely you will see the error. 
 

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1893
Re: double representation
« Reply #18 on: March 01, 2012, 11:09:19 am »
That problem with strtod comes from crt function __fppow10 ???
So i test normal pow()
Code: [Select]
double pow(double, double);
int __cdecl mainCRTStartup(void)
{
double d1,d2,d3;
d1 = 200000000.0;
#ifdef TESTPOS
d2 = pow(10, 5);
d3 = d1 / d2;
#else
d2 = pow(10, -5);
d3 = d1 * d2;
#endif
unsigned long long *ll = (unsigned long long *)&d3;
return 0;
}
With pow(10,5) ll is 0x409F400000000000
and with pow(10,-5) ll is 0x409F400000000001
Can anyone examine that ???
May the source be with you

czerny

  • Guest
Re: double representation
« Reply #19 on: March 01, 2012, 09:42:20 pm »
Just to get it clear!

The double 2000.0 in the original posting has not seen a fpu at all. So the observed value is produced during compiletime. Wrong or right?

czerny


oog

  • Guest
Re: double representation
« Reply #20 on: March 04, 2012, 05:25:33 pm »
That's for Pelle to say...
I see.  Sounds good.  Thanks Tater.

CommonTater

  • Guest
Re: double representation
« Reply #21 on: March 05, 2012, 01:12:49 am »
That's for Pelle to say...
I see.  Sounds good.  Thanks Tater.

No worries... This whole "last bit" thing has been rattling around for at least 15 years... so sometimes what looks like a bug is a hardware problem... or visa versa.  Pelle is the only one with access to the source code, so it falls to him....  Sorry I couldn't be more help.



George99

  • Guest
Re: double representation
« Reply #22 on: March 09, 2012, 08:41:31 pm »
Is that part of a standard someplace?
I ask, because this is the first I've heard of it and I do like to have copies of these things on hand.
Hi CommomTater,

maybe this excerpt of the wiki article of IEEE 754 explains it well:

Quote
Any integer with absolute value less than or equal to 224 can be exactly represented in the single precision format, and any integer with absolute value less than or equal to 253 can be exactly represented in the double precision format. Furthermore, a wide range of powers of 2 times such a number can be represented. These properties are sometimes used for purely integer data, to get 53-bit integers on platforms that have double precision floats but only 32-bit integers.
http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers

CommonTater

  • Guest