Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Arithmetic

Reply
Thread Tools

Arithmetic

 
 
Boris Punk
Guest
Posts: n/a
 
      07-07-2010
long l = 9999999999999L;
double f = 0.11111111111D;
double fl = f+l;
System.out.println(fl);

=9.999999999999111E12


Where's the rest of the 0.1111111's ?


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-07-2010
Boris Punk wrote:
> long l = * 9999999999999L;
> double f = 0.11111111111D;
> double fl = f+l;
> System.out.println(fl);
>
> =9.999999999999111E12
>
> Where's the rest of the 0.1111111's ?
>


This is a FAQ. Don't they teach numerical analysis at university any
more? What's with the education system these days anyway?

Your answer is here:
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>

--
Lew

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      07-07-2010
On 07/07/2010 12:37 PM, Boris Punk wrote:
> long l = 9999999999999L;
> double f = 0.11111111111D;
> double fl = f+l;
> System.out.println(fl);
>
> =9.999999999999111E12
>
>
> Where's the rest of the 0.1111111's ?


Double has a 53-bit mantissa, which corresponds to around 15 decimal
digits of precision.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-07-2010
On 7/7/2010 12:37 PM, Boris Punk wrote:
> long l = 9999999999999L;
> double f = 0.11111111111D;
> double fl = f+l;
> System.out.println(fl);
>
> =9.999999999999111E12
>
>
> Where's the rest of the 0.1111111's ?



double f1 = 1;
double f9 = 9;
double fq = f1 / f9;
System.out.println(fq);

0.1111111111111111

Where's the rest of the 0.1111111's ?

(Answer: They went where your missing 1's went.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Mayeul
Guest
Posts: n/a
 
      07-07-2010
Lew wrote:
> Boris Punk wrote:
>> long l = 9999999999999L;
>> double f = 0.11111111111D;
>> double fl = f+l;
>> System.out.println(fl);
>>
>> =9.999999999999111E12
>>
>> Where's the rest of the 0.1111111's ?
>>

>
> This is a FAQ. Don't they teach numerical analysis at university any
> more? What's with the education system these days anyway?
>
> Your answer is here:
> <http://docs.sun.com/source/806-3568/ncg_goldberg.html>


Come on, if that did not lead to horribly broken code out in production,
it would be a great IT achievement that you can produce new programs
that work, without understanding what you're programming.

--
Mayeul
 
Reply With Quote
 
Boris Punk
Guest
Posts: n/a
 
      07-07-2010

This is a FAQ. Don't they teach numerical analysis at university any
more? What's with the education system these days anyway?



Didn't in Software Dev. Objects n diagrams in dev.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-07-2010
Lew wrote:
>> Your answer is here:
>> <http://docs.sun.com/source/806-3568/ncg_goldberg.html>

>


Mayeul wrote:
> Come on, if that did not lead to horribly broken code out in production,
> it would be a great IT achievement that you can produce new programs
> that work, without understanding what you're programming.
>


You said it yourself - it leads to horribly broken code out in
production that people produce new programs without understanding what
they're programming.

The reason that computer programmers command such good wages is that
it is a *skilled* profession. Most people cannot do it, and of those
who can it requires intelligence, study and practice, i.e., it
requires tremendous intellectual effort and capacity.

It is one of the most fundamental and introductory aspects of computer
programming that floating-point "numbers" in a computer are limited-
precision approximations of real numbers.

Mayeul, I'm not sure what point you were actually making. I sincerely
hope you are not arguing in favor of programmers not understanding
what they're doing.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-07-2010
Please set off citations, traditionally with leading ">" characters,
and cite the source.

Lew wrote:
> This is a FAQ. *Don't they teach numerical analysis at university any
> more? *What's with the education system these days anyway?
>


Boris Punk wrote:
> Didn't in Software Dev. Objects n diagrams in dev.
>


Whatever you said, I commend you heartily for making up the gap.

(What is "Software Dev. Objects n diagrams in dev"? That was well-
nigh incomprehensible.)

--
Lew
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      07-07-2010
On 07-07-2010 12:37, Boris Punk wrote:
> long l = 9999999999999L;
> double f = 0.11111111111D;
> double fl = f+l;
> System.out.println(fl);
>
> =9.999999999999111E12
>
> Where's the rest of the 0.1111111's ?


Floating point math is not always exact.

The floating point types in Java has a fixed
size and is therefore limited to a certain precision.

The 9999999999999 is converted to 9999999999999.0
before the addition is happening.

9999999999999.0 + 0.11111111111

in floating point should be read as:

something between 9999999999998.995 and 9999999999999.005 plus
something between 0.1111111111099995 and 0.1111111111100005
and the result something between 9999999999999.105 and 9999999999999.115
is correct.

Arne

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      07-07-2010
On 07-07-2010 14:13, Lew wrote:
> The reason that computer programmers command such good wages is that
> it is a *skilled* profession. Most people cannot do it, and of those
> who can it requires intelligence, study and practice, i.e., it
> requires tremendous intellectual effort and capacity.


Why can I hear violins in the background?



> It is one of the most fundamental and introductory aspects of computer
> programming that floating-point "numbers" in a computer are limited-
> precision approximations of real numbers.


There are actually programmers that are never exposed to floating
point.

Arne

 
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
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Java Arithmetic - Using MOD (%) Steven Davies Java 3 02-01-2005 10:25 PM
Performing Arithmetic on DataTable Columns George Durzi ASP .Net 2 06-07-2004 05:23 PM
Arithmetic Libraries ALuPin VHDL 1 02-10-2004 04:37 PM
Synthesisable fixed-point arithmetic package Jonathan Bromley VHDL 1 08-15-2003 03:32 PM



Advertisments