Home static Java Programming [Archive] - floating point
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - floating point
 This topic has 37 replies on 3 pages.    « Previous | 1 | 2 | 3 |

Posts:5,965
Registered: 5/17/03
Re: floating point
Jul 29, 2004 9:06 PM (reply 30 of 37)

also, how does Double.toString(double) rounds and make
it to 0.1?

I'll give you the basic idea (algoritm). Say you have 145. To extract the rightmost decimal digit you use modulo (%) 10. This will give you the 5. Then you remove this digit using an integer division (/) with 10. You're left with 14. Then you repeat the above steps to extract the 4 and the 1. This series of numbers between 0 and 9 are then turned into char representation (another 16 bit number).

You have many relevant questions and of course you're wellcome to ask them here, but I suggest you enroll in some basic computer science course to get the whole picture. A solid grounding will help you immensely later.

Posts:236
Registered: 5/11/04
Re: floating point
Jul 29, 2004 9:23 PM (reply 31 of 37)

I'll give you the basic idea (algoritm). Say you have 145. To extract the rightmost decimal digit you use
modulo (%) 10. This will give you the 5. Then you remove this digit using an integer division (/) with 10.
You're left with 14. Then you repeat the above steps to extract the 4 and the 1. This series of numbers
between 0 and 9 are then turned into char representation (another 16 bit number).

I've understand so many points already ,as you, jverd and others patiently explaining there are points that I can't seem to understand...

just the one below.

how Double.toString(double) decides when to round the number..

say for example;

double d1 = 0.1;
Double.toString(d1);

this, as we all know, 0.1 is not really stored in d1 and the value passed to the toString(d1) is not really 0.1.
what actually passed is the "nearest" decimal representation of 0.1, say it is 0.0999999999999999......

now, if the value passed is "0.0999999999999999......", how the java decides that,
oh, i'll round it to 0.1 since it is the nearest value, and I will return a string representaion of "0,1"..

then when the value is d1 = 0.1 + 0.1 + 0.1, which will not become exactly "0.3", comes to toString(d1),
then, java prints "0.30000000000000004".

sorry, if this is really specs..as jverd said...

thank you so much.

You have many relevant questions and of course you're wellcome to ask them here, but I suggest you enroll in some basic computer science course to get the whole picture. A solid grounding will help you immensely later.

Posts:236
Registered: 5/11/04
Re: floating point
Jul 29, 2004 9:24 PM (reply 32 of 37)

You have many relevant questions and of course you're wellcome to ask them here, but I suggest you
enroll in some basic computer science course to get the whole picture. A solid grounding will help you
immensely later.

actually, i had computer science background but this one really drives me crazy..sorry...

Posts:37,103
Registered: 3/30/99
Re: floating point
Jul 29, 2004 9:50 PM (reply 33 of 37)

actually, i had computer science background wiki/./but this
one really drives me crazy..sorry...

Yeah, it's just one of those things that you have to get, but it is kind of tricky.

Posts:5,965
Registered: 5/17/03
Re: floating point
Jul 29, 2004 9:52 PM (reply 34 of 37)

sorry, if this is really specs..as jverd said...

Yes it's in the IEEE 754 floating-point specification and you've also been given relevant links in this thread explaining both principles and details.

Posts:37,103
Registered: 3/30/99
Re: floating point
Jul 29, 2004 9:58 PM (reply 35 of 37)

how Double.toString(double) decides when to round the
number..

It always rounds it. There's probably a fixed number of digits in the source code somewhere, and an agorithm as UJ described is applied for that many digits. I'm not sure, but it may be that it's for that many digits plus one, with that extra digit being used for rounding.

Posts:236
Registered: 5/11/04
Re: floating point
Jul 29, 2004 9:58 PM (reply 36 of 37)

thanks you jverd, UJ and the rest...

i've wanted to give more dukes but i am running out already..

see you again in one of my future posts... :-)

Posts:5,965
Registered: 5/17/03
Re: floating point
Jul 29, 2004 10:55 PM (reply 37 of 37)

Thanks and good luck -:)

 This topic has 37 replies on 3 pages.    « Previous | 1 | 2 | 3 |