Arithmetic

B

Boris Punk

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

Joshua Cranmer

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

Eric Sosman

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

Boris Punk

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

Lew

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

Lew

Please set off citations, traditionally with leading ">" characters,
and cite the source.
This is a FAQ.  Don't they teach numerical analysis at university any
more?  What's with the education system these days anyway?

Boris said:
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.)
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
B

Boris Punk

Arne Vajhøj said:
Why can I hear violins in the background?

:)


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

Lew

Boris said:
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.
 
R

Roedy Green

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)
 
J

Jukka Lahtinen

Boris Punk said:
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.
 
M

Mayeul

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

Andreas Leitgeb

Mayeul said:
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.
 
A

Arved Sandstrom

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top