# Arithmetic

Discussion in 'Java' started by Boris Punk, Jul 7, 2010.

1. ### Boris PunkGuest

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 ?

Boris Punk, Jul 7, 2010

2. ### LewGuest

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?

<http://docs.sun.com/source/806-3568/ncg_goldberg.html>

--
Lew

Lew, Jul 7, 2010

3. ### Joshua CranmerGuest

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

Joshua Cranmer, Jul 7, 2010
4. ### Eric SosmanGuest

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
lid

Eric Sosman, Jul 7, 2010
5. ### MayeulGuest

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

Mayeul, Jul 7, 2010
6. ### Boris PunkGuest

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.

Boris Punk, Jul 7, 2010
7. ### LewGuest

Lew wrote:
>> <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

Lew, Jul 7, 2010
8. ### LewGuest

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

Lew, Jul 7, 2010
9. ### Arne VajhøjGuest

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

Arne Vajhøj, Jul 8, 2010
10. ### Arne VajhøjGuest

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

Arne Vajhøj, Jul 8, 2010
11. ### LewGuest

Lew wrote:
>> 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.

Arne VajhÃ¸j wrote:
> There are actually programmers that are never exposed to floating
> point.

Then a) they aren't programmers and b) they are derelict if they wish to be.

--
Lew

Lew, Jul 8, 2010
12. ### Boris PunkGuest

"Arne Vajhøj" <> wrote in message
news:4c350cb7\$0\$282\$...
> 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
>
>

I did plenty of manual sums of floating point numbers at college on paper -
just wanted a refresher from some knowlegeable folk on how it's applied in
java. Is Lew on a period or something? Hehe get it...period...never mind....

Boris Punk, Jul 8, 2010
13. ### Boris PunkGuest

"Joshua Cranmer" <> wrote in message
news:i12c6n\$gpt\$...
> 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
>

thank you

Boris Punk, Jul 8, 2010
14. ### LewGuest

Boris Punk wrote:
> I did plenty of manual sums of floating point numbers at college on paper -
> just wanted a refresher from some knowlegeable folk on how it's applied in
> java [sic]. Is Lew on a period or something? Hehe get it...period...never mind....

Are you suggesting that there's something wrong with programmers knowing the
basics of our profession? Or with espousing that we do?

Seems to me that someone with a commitment to excellence in the programming
profession would agree that we should improve our skills, study the basics,
and strive for superiority in our professionalism, rather than make sexist,
low-brow remarks about someone who makes those points.

--
Lew

Lew, Jul 8, 2010
15. ### Roedy GreenGuest

On Wed, 07 Jul 2010 19:48:24 +0200, Mayeul <>
wrote, quoted or indirectly quoted someone who said :

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

I used use to teach FORTRAN at UBC way back when. We covered this in
the first semester. This very basic stuff about floating point. If
you taught yourself you may have missed this. See
http://mindprod.com/jgloss/floatingpoint.html
to fill in your missing background.
--
Roedy Green Canadian Mind Products
http://mindprod.com

There is no harm in being sometimes wrong especially if one is promptly found out.
~ John Maynard Keynes (born: 1883-06-05 died: 1946-04-21 at age: 62)

Roedy Green, Jul 8, 2010
16. ### Jukka LahtinenGuest

"Boris Punk" <> writes:

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

The double primitive can't generally hold precise values for fractions.
Whenever you need precision for fractions, use the BigDecimal class
instead of double. When precision is needed for integers bigger than
what long can hold, use BigInteger.

--
Jukka Lahtinen

Jukka Lahtinen, Jul 8, 2010
17. ### MayeulGuest

Roedy Green wrote:
> On Wed, 07 Jul 2010 19:48:24 +0200, Mayeul <>
> wrote, quoted or indirectly quoted someone who said :
>
>> 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.

>
> I used use to teach FORTRAN at UBC way back when. We covered this in
> the first semester. This very basic stuff about floating point. If
> you taught yourself you may have missed this. See
> http://mindprod.com/jgloss/floatingpoint.html
> to fill in your missing background.

I did not miss much from what the given URL tells about, I'm afraid. (Or
not.)

--
Mayeul

Mayeul, Jul 8, 2010
18. ### MayeulGuest

Lew wrote:
> Lew wrote:
>>> <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.
>>

> [...]
> 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.

Nah, I was being sarcastic. Well, that *would* be awesome if computers
just listened to what we want and then programmed themselves, but I
don't see it happening for real any soon .

To be honest, when I see what can be achieved by people who have close
to no idea how the maths hold it all together, I wonder why I dream so
much that all my colleagues would have had a full computer sciences
education. Well, in that dream world of mine, some silly mistakes
leading to horrible production issues would never have happened.
Does not mean all silly mistakes are due to that though.

I'm convincing myself there was no sarcasms to make. Oh well. I'd better
stop.

--
Mayeul

Mayeul, Jul 8, 2010
19. ### Andreas LeitgebGuest

Mayeul <> wrote:
> Roedy Green wrote:
>> I used use to teach FORTRAN at UBC way back when. We covered this in
>> the first semester. This very basic stuff about floating point. If
>> you taught yourself you may have missed this. See
>> http://mindprod.com/jgloss/floatingpoint.html
>> to fill in your missing background.

> I did not miss much from what the given URL tells about, I'm afraid. (Or
> not.)

A quote from the page:
" It is probably best if you imagined that after every floating point
" operation, a little demon came in and added or subtracted a tiny number
" to fuzz the low order bits of your result.

I like that thought

PS: before anyone comments on technical inaccuracies in the quote, please
read up its original context at Roedy's URL, first.

Andreas Leitgeb, Jul 8, 2010
20. ### Arved SandstromGuest

Lew wrote:
> Lew wrote:
>>> <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

The problem is more fundamental. You can't properly teach floating
point, or a bunch of other necessary programming concepts, without
having students who are proficient at math. We're talking about the kind
of math that underlies all of programming, not particular types of it
that are necessary for specific problem domains.

You can try to assign blame for not being good at math to the
individual, but I blame the public school educational systems we have.
It's not a child's job to know what he or she should know, it's the
schools' job. And they're not doing very well at it. By the time we -
employers, universities and colleges, IT programmes - receive those
prospective programmers, we have a right to expect that they should know
the basics. And more often than not they don't.

Another factor at play, and Arne alluded to it, is that a lot of
programmers - probably the majority - never have to worry about floating
point issues. They'll never get bitten. The biggest source of potential
problems for the general-purpose programmer is likely handling money,
but if there's one pervasive piece of programming CW that even newbies
hear about it's the advice to use integers rather than floating point.
Hence they never have to think about it.

I'm prepared to bet that considerably less than 5 percent of all
professional programmers have thoroughly read _any_ of the handful of
well-known papers on floating point arithmetic.

AHS

--
Sir James Dewar
Is smarter than you are
None of you asses
Can liquify gases.

Arved Sandstrom, Jul 8, 2010