Arithmetic

S

Screamin Lord Byron

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.

Why not simply read Wiki's article about IEEE 754. If floating point
calculations must be precise and predictable across platforms, one must
ensure to use IEEE 754 (ie. strictfp keyword), and not the default,
machine-dependent fp representation.
 
M

Martin Gregorie

Why can I hear violins in the background?

:)


There are actually programmers that are never exposed to floating point.
.... but there are integer-only programmers who never 'get' computer
arithmetic. I once worked with a COBOL 'programmer' who never understood
why divide by zero would trigger an ON SIZE ERROR clause or crash the
program if the DIVIDE sentence didn't contain ON SIZE ERROR. Various
people tried to explain it to him / pound it into his head but he was
totally resistant to the concept.
 
M

Martin Gregorie

Then a) they aren't programmers and b) they are derelict if they wish to
be.
Depends what they're doing, e.g. if they're writing financial software in
COBOL or RPG 3, its quite likely that they may never need use floating
point in their entire working life: by default COBOL does fixed decimal
point arithmetic which is more appropriate for financial calculations
than floating point because the answer is always exact. You'll find that
central banks and international networks such as SWIFT have mandatory
rules for currency conversion that specify the required precision and how
rounding and or remainders are to be handled.

However, COBOL introduces its own arithmetic oddities because its really
using integer arithmetic with an implied decimal point. For example, the
correct answer to dividing 0.4 by 2.0 using PIC S9(6)v9(6) variables is a
result of 0.0 with a remainder of 0.4 because its actually dividing
400000 by 2000000. This is spelt out in the COBOL specification - and is,
of course, exactly how BigDecimal handles division if you use the
divideAndRemainder() method.
 
T

Tom Anderson

Depends what they're doing, e.g. if they're writing financial software in
COBOL or RPG 3, its quite likely that they may never need use floating
point in their entire working life: by default COBOL does fixed decimal
point arithmetic which is more appropriate for financial calculations
than floating point because the answer is always exact.

Exact, although not necessarily correct!

tom
 
C

Chase Preuninger

They exist still in memory. Then you call System.out.println(f1) it
is really like calling System.out.println(String.valueOf(f1)) the
valueOf() function is creating its own representation of the number.
Try using some of the number format classes in the java.text package.
 
E

Eric Sosman

They exist still in memory. Then you call System.out.println(f1) it
is really like calling System.out.println(String.valueOf(f1)) the
valueOf() function is creating its own representation of the number.
Try using some of the number format classes in the java.text package.

(Somewhere in the context lost to overzealous snippage, Chase
is answering "Where's the rest of the 0.1111111's?" from an O.P.
who printed the result of `9999999999999L + 0.11111111111D'.)

The missing low-order digits do *not* "exist still in memory,"
as others have explained. A Java `double' has 53 bits' worth of
precision, just a little less than 16 decimal digits. The O.P.'s
24 decimal digits are far too many for `double', and the lost bits
exist nowhere (except in fevered imaginations).
 
L

Lew

Eric said:
     (Somewhere in the context lost to overzealous snippage, Chase
is answering "Where's the rest of the 0.1111111's?" from an O.P.
who printed the result of `9999999999999L + 0.11111111111D'.)

     The missing low-order digits do *not* "exist still in memory,"
as others have explained.  A Java `double' has 53 bits' worth of
precision, just a little less than 16 decimal digits.  The O.P.'s
24 decimal digits are far too many for `double', and the lost bits
exist nowhere (except in fevered imaginations).

Once again, as computer people we should all be familiar with the
information in
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>
 
M

Martin Gregorie

Exact, although not necessarily correct!
.....which is why exchange rate calculations for SWIFT and the Euro, to
name but two, are spelt out in considerable detail and correct
implementation of them is part of the certification tests required for
connection to the relevant networks.
 
J

Jim Janney

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 short and grossly oversimplified answer is that there are an
infinite number of real numbers and we can't possibly represent them
all with any finite alphabet. Floating point is a notation that
allows us to represent a subset of the real numbers, a subset that in
practice often contains values that are "close enough", where "close
enough" depends heavily on the problem at hand. Unfortunately this
subset is not closed under arithmetic: many simple operations can
generate values outside of the subset that can be represented. A good
implementation of floating point arithmetic will always give you the
representable value closest to the actual result: in practice you
can't always count even on this, although it shouldn't be too far off.

For the long answer I recommend taking a class on axiomatic set theory
(one of the best math classes I ever took). Rational numbers are
significantly different from integers, and real numbers are truly
weird.
 
J

Jim Janney

Tom Anderson said:
Exact, although not necessarily correct!

Financial calculations, especially ones involving interest rates,
often use values that are expressed as decimal fractions, 0.01, 0.03,
etc. These can be represented exactly in fixed or floating decimal,
but usually not in binary. 0.125 can, but it's an exception.

Every so often I have a go at trying to explain this to my coworkers,
so far without much success.
 
J

Jim Janney

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

I'm with Lew on this one: it's an important subject and I meet too
many programmers, many with CS degrees, who not only don't understand
it but don't even seem to know that it exists.

I learned enough numerical analysis to understand how little I know
about it: many programmers don't seem to know even that.
 
A

Arne Vajhøj

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

There are lots of fields where floating point is never used.

I consider it a bit arbitrary to consider programmers with
narrow focus on business apps or embedded apps not to be programmers
a bit arbitrary.

Arne
 
A

Arne Vajhøj

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

I'm with Lew on this one: it's an important subject and I meet too
many programmers, many with CS degrees, who not only don't understand
it but don't even seem to know that it exists.

I learned enough numerical analysis to understand how little I know
about it: many programmers don't seem to know even that.

Numerical analysis is not a big CS topic.

You may find more experience with it in the other sciences: physics,
astronomy, statistics, econometrics etc..

Arne
 
A

Arne Vajhøj

Why not simply read Wiki's article about IEEE 754. If floating point
calculations must be precise and predictable across platforms, one must
ensure to use IEEE 754 (ie. strictfp keyword), and not the default,
machine-dependent fp representation.

Note that Java always use IEEE 754 representation for storage of
floating point numbers. The strictfp keyword only ensures that
intermediate results in calculations also are represented that way.

Arne
 
L

Lew

There are lots of fields where floating point is never used.

I consider it a bit arbitrary to consider programmers with
narrow focus on business apps or embedded apps not to be programmers
a bit arbitrary.

I consider people who call themselves "programmers" with only a very narrow
understanding of the field to be poseurs.

It's a "tip of the iceberg" thing. You should know more than you use. I
don't say one should know everything about everything in programming, but
there's a reason the seminal article on floating-point computing is named,
"What *Every* Computer Scientist Should Know About Floating-Point Arithmetic"
[emph. added]. OK, I extend that to every programmer, but come on! One
should at least be aware of the basics.

I am shocked and horrified at the apologistics for ignorance in this thread.
For shame!
 
K

Kevin McMurtrie

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

:)


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

Arne

Paid programmers? Computer science is knowing how information from the
real world can be represented within the constraints of a computer.
It's the basis for knowing what you're doing while programming.
 
B

blmblm

[ snip ]

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

When you were doing those manual sums, was part of the problem
specification how many digits of precision would be kept? I guess
I'm genuinely curious about whether you just didn't remember that
floating-point quantities have limited precision [*], or you remembered
that but somehow thought the Java "double" primitive type might be
different, or what.

[*] Now that I think about it, I'm uncertain about whether that's
inherent in a definition of "floating point" -- the definition
used in the Wikipedia article on "floating point" seems to imply
fixed size, but to me that doesn't seem to be inherent in the
idea of "binary representation of scientific notation", though
it might be more difficult to build hardware that could operate
on floating-point quantities of arbitrary size.
Is Lew on a period or something? Hehe get it...period...never mind....

Clever wordplay, in its way, but I'd advise against jokes that rely
on gender stereotypes, unless you know more about your audience than
I think you do here.
 
M

Martin Gregorie

Financial calculations, especially ones involving interest rates, often
use values that are expressed as decimal fractions, 0.01, 0.03, etc.
These can be represented exactly in fixed or floating decimal, but
usually not in binary. 0.125 can, but it's an exception.

Every so often I have a go at trying to explain this to my coworkers, so
far without much success.

Actually, I've yet to see a financial package that used anything but
integers for currency amounts, so in USD or GBP the amounts would be
cents and pence respectively the decimal point is just interpolated when
the value is displayed, e.g. in COBOL:

01 AMOUNT COMP SYNC PIC S9(9).
01 DISP_AMOUNT PIC -Z,ZZZ,ZZ9.99 BLANK WHEN ZERO.

MOVE 150 TO AMOUNT.
......
MOVE AMOUNT TO DISP-AMOUNT.

would output the amount, $1.50 held internally as 150 cents, as
" 1.50"

Interest and exchange rates, etc. is quite another matter: whether they
are held internally as fixed point decimal or floating point is entirely
up to the system designer and the compiler writer.
 
M

Martin Gregorie

You may find more experience with it in the other sciences: physics,
astronomy, statistics, econometrics etc..
I'm glad you said *may* as I remember meeting an actuary, no less, who
insisted on calculating standard deviations on samples containing less
than 10 values. When, as a rather junior programmer but one with an MSc,
I queried this I got slapped down by said actuary.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top