code for fun

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

code for fun

aaron.lewis1989 (Bugzilla)
-----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-----

Reply | Threaded
Open this post in threaded view
|

Re: code for fun

Firas Kraiem-4
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

Reply | Threaded
Open this post in threaded view
|

OT: Re: code for fun

aaron.lewis1989 (Bugzilla)
-----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-----

Reply | Threaded
Open this post in threaded view
|

Re: OT: Re: code for fun

Floor Terra
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/

Reply | Threaded
Open this post in threaded view
|

Re: OT: Re: code for fun

Firas Kraiem-4
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 <stdint.h>
#include <stdio.h>

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

Reply | Threaded
Open this post in threaded view
|

Re: OT: Re: code for fun

aaron.lewis1989 (Bugzilla)
In reply to this post by Floor Terra
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07/18/2010 08:04 PM, Floor Terra wrote:
> 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:
>>  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?!

See:

http://en.wikipedia.org/wiki/Arithmetic_precision

>
>>
>> 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!

Sorry , they're all signed.

>
>>
>> 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?
>
>

Come on , just for fun , someone asks me if i could print out the
maximum number that can be stored in a double type , and the *precision*
of a double type.

For the first , maybe with some bit operations , no good idea from me.

And the second , well i tried to divide 2 by 3 , see when i reaches 7 (
should be 0.66....7 ) ,
may not a clever way.


- --
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/

iEYEARECAAYFAkxC9EsACgkQvf41sEptMqD8CACfVFw2rSxQHBs1u8hLgltLIEr3
heUAniQgV+JEyy/WLr+IsV3mtIGUznM+
=xwd7
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: OT: Re: code for fun

Floor Terra
On Sun, Jul 18, 2010 at 2:32 PM, Aaron Lewis <[hidden email]>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 07/18/2010 08:04 PM, Floor Terra wrote:
>> 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?!
>
> See:
>
> http://en.wikipedia.org/wiki/Arithmetic_precision

Yes. 12.340 has five significant digits. If you want to store it with
just two significant digits you get 12.

> Come on , just for fun , someone asks me if i could print out the
> maximum number that can be stored in a double type , and the *precision*
> of a double type.

The maximum number you can store in a IEEE double is +infinity of course.

A 64 bit double has 52-bit mantissa, together with 1 sign bit it gives
you almost 16 decimal significant digits.
But like other things in life: It's not the size that maters, it's how
you use it. (see math(3) and look for ULP)

>
> And the second , well i tried to divide 2 by 3 , see when i reaches 7 (
> should be 0.66....7 ) ,
> may not a clever way.

Be careful converting  your number to decimal if you want to keep
maximum precision.


--
Floor Terra <[hidden email]>
www: http://brobding.mine.nu/