# code for fun Classic List Threaded 7 messages Open this post in threaded view
|

## code for fun

 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi,         how can i determine the maximum size of `double' without printing out DOUBLE_MAX ?         And what about the precision of a `double' , anyone give a try ?         Google ain't giving much information , i just can't figure out , it's very funny coding , isn't it ? - -- Best Regards, Aaron Lewis - PGP: 0x4A6D32A0 FingerPrint EA63 26B2 6C52 72EA A4A5 EB6B BDFE 35B0 4A6D 32A0 irc: A4r0n on freenode Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/iEYEARECAAYFAkxC1ykACgkQvf41sEptMqCjTwCgisIxQB0fIOCQSrlfhIn7GShT JogAnAsqx2g5M+PyE0DZLNeSHF1luiYc =0LIw -----END PGP SIGNATURE-----
Open this post in threaded view
|

## Re: code for fun

 On 18/07/2010 12:27, Aaron Lewis wrote: > Hi, > how can i determine the maximum size of `double' without printing out > DOUBLE_MAX ? > > And what about the precision of a `double' , anyone give a try ? > > Google ain't giving much information , i just can't figure out , it's > very funny coding , isn't it ? > Kinda off-topic for here but whatever... Assuming 64-bit double, it has a mantissa of 52 bits and an exponent of 11 bits. If the mantissa is all-ones, that will give a significant of 2-2^(-52).  The exponent cannot be all-ones (an all-ones exponent means infinity or NaN), so the largest exponent is 2^11-2 = 2046, minus bias (1023), it gives a maximum effective exponent of 1023, so that would give a value of (2-2^(-52))*2^(1023) = 2^1024 - 2^971, roughly 10^308. What do you mean by "precision"? Firas
Open this post in threaded view
|

## OT: Re: code for fun

 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 07/18/2010 06:56 PM, Firas Kraiem wrote: > On 18/07/2010 12:27, Aaron Lewis wrote: >> Hi, >> how can i determine the maximum size of `double' without printing out >> DOUBLE_MAX ? >> >> And what about the precision of a `double' , anyone give a try ? >> >> Google ain't giving much information , i just can't figure out , it's >> very funny coding , isn't it ? >> > > Kinda off-topic for here but whatever... Yeah , i'll put a OT here. > > Assuming 64-bit double, it has a mantissa of 52 bits and an exponent of > 11 bits. If the mantissa is all-ones, that will give a significant of > 2-2^(-52).  The exponent cannot be all-ones (an all-ones exponent means > infinity or NaN), so the largest exponent is 2^11-2 = 2046, minus bias > (1023), it gives a maximum effective exponent of 1023, so that would > give a value of (2-2^(-52))*2^(1023) = 2^1024 - 2^971, roughly 10^308. Yep , right , but how you *get* it by a beautiful code  , that could be really interesting. I mean , with a small code , c or c++ , printing the size out. > > What do you mean by "precision"? Kinda of significance digit. For example:  A number `12.340' , and if say it has 2 digits' precision , then we consider the `0' is not accurate , while `.34' is accurate. So for a number stored in a double type , how accurate can it be ? (or maybe how many bits in the fixed-point part is accurate) Doesn't matter if it's unsigned or signed , some ideas are cool enough. Still , use a small part of code , c or c++ , i'm just curious how to make it happen. > > Firas - -- Best Regards, Aaron Lewis - PGP: 0x4A6D32A0 FingerPrint EA63 26B2 6C52 72EA A4A5 EB6B BDFE 35B0 4A6D 32A0 irc: A4r0n on freenode Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/iEYEARECAAYFAkxC4IkACgkQvf41sEptMqBN3wCeNNWj/cs9b1SB4hXwxqnJQrR4 gxsAoLKTzixeHSQzWHAeKTzgp6WDPn3I =McSS -----END PGP SIGNATURE-----
Open this post in threaded view
|

## Re: OT: Re: code for fun

 On Sun, Jul 18, 2010 at 1:07 PM, Aaron Lewis <[hidden email]> wrote: >> What do you mean by "precision"? > > Kinda of significance digit. For example: > B A number `12.340' , and if say it has 2 digits' precision , then we > consider the `0' is not accurate , while `.34' is accurate. What?! > > So for a number stored in a double type , how accurate can it be ? (or > maybe how many bits in the fixed-point part is accurate) http://en.wikipedia.org/wiki/Double_precision_floating-point_format> > Doesn't matter if it's unsigned or signed , some ideas are cool enough. I'd like to see your unsigned double! > > Still , use a small part of code , c or c++ , i'm just curious how to > make it happen. What problem (i.e. homework assignment) are you trying to solve? -- Floor Terra <[hidden email]> www: http://brobding.mine.nu/
Open this post in threaded view
|

## Re: OT: Re: code for fun

 In reply to this post by aaron.lewis1989 (Bugzilla) > > Assuming 64-bit double, it has a mantissa of 52 bits and an exponent of > > 11 bits. If the mantissa is all-ones, that will give a significant of > > 2-2^(-52).  The exponent cannot be all-ones (an all-ones exponent means > > infinity or NaN), so the largest exponent is 2^11-2 = 2046, minus bias > > (1023), it gives a maximum effective exponent of 1023, so that would > > give a value of (2-2^(-52))*2^(1023) = 2^1024 - 2^971, roughly 10^308. > > Yep , right , but how you *get* it by a beautiful code  , that could be > really interesting. > > I mean , with a small code , c or c++ , printing the size out. Since you know the bit pattern that would yield the value, just make an union with an uint64_t and a double, set the uint to the desired bit-pattern, and printf the double: #include #include int main(void) {         union intdouble {                 uint64_t foo;                 double bar;         } maxdouble;         maxdouble.foo = 0x7FEFFFFFFFFFFFFF;         printf("%lf\n", maxdouble.bar);         return 0; } > > > > What do you mean by "precision"? > > Kinda of significance digit. For example: > A number `12.340' , and if say it has 2 digits' precision , then we > consider the `0' is not accurate , while `.34' is accurate. > > So for a number stored in a double type , how accurate can it be ? (or > maybe how many bits in the fixed-point part is accurate) It depends on the exponent.  If the exponent is large (as above), you won't get anything after the decimal point.  The maximum you can get after the decimal point is when you have a denormalized number (exponent all-zeroes) and a mantissa of 000...1.  That will give an exponent of -1022 and a significant of 2^(-52), yielding a value of 2^(-52)*2^(-1022) = 2^(-1074), roughly 10^(-324). > Doesn't matter if it's unsigned or signed , some ideas are cool enough. IEEE floating-point numbers are always signed. Firas