views:

112

answers:

4

I was trying my hand at Google Code Jam and there was a question that used double values...

I had a hard time coding in C, always somehow my answer would differ from the actual answer in some cases...

What I wanted to know is that which language has the best floating point implementation and handling?

This has happened a numerous times and I feel it is time I switch to a language that better handles floats. Any ideas?

+4  A: 

IEEE 754 specifies "everything you need to know" about floating point numbers. It's not a matter or which programming language you use so much as the compiler/hardware involved.

If you want precise non-integer numbers, you need to use a Decimal class such as that provided by .NET or Python or Java, for instance.

Mark Rushakoff
Decimal types are no more precise than regular floats, except that they happen to be precise when expressing fractions in decimal. This is sort of an issue of UI.
TokenMacGuy
+5  A: 

Floating-point is pretty much the same across languages these days, because IEEE 754 is used almost universally.

So it is likely that you need to get a better understanding of floating point numbers, for example by reading What Every Computer Scientist Should Know About Floating-Point Arithmetic, by David Goldberg.

starblue
+2  A: 

fp in almost every language is delegated to the hardware. Doing floating point calculations is expensive, but useful, and so almost all modern processors have some kind of built in logic for those calculations. It would be very silly for a langauge to reinvent this particular wheel.

That being said, there are a few things you can do, depending on what you want out of your FP calculations. If you need higher precision that is typically available on double precision hardware, you can use some libraries that do reinvent the wheel, perhaps because you must solve a particularly ill conditioned system of equations for instance, GMP provides a number of number formats with arbitrary precision. This will be about an order of magnitude slower than hardware FP, but maybe you need that.

If you're running afoul of the 1.0/10.0 == 10.000000002 issue, probably because you are trying to manipulate currency, then you need to be doing your calculations differently, for instance many languages provide a special number class just for working with decimal numbers in ways most accountants would expect. You could also use the above mentioned bignum library.

If you need vastly more computing resources, Modern GPU's are equipped with dozens or hundreds of fp cores, and now provide some very convenient API's for accessing them. You might want to take a look at OpenCL, which gives a portable abstraction for that sort of solution.

TokenMacGuy
+1  A: 

What I wanted to know is that which language has the best floating point implementation and handling?

Either Fortran or assembler; many others don't have standardised mechanisms for accessing all the types ( 80 bit reals and so on ) and the processor rounding modes ( usually also available as compiler extensions in C ).

However, floating point is not infinitely precise, and multiple precision values can require infinite space to represent certain values.

Another approach for some expressions would be scheme or other lisp family languages which support rational numbers - so 1/3 is represented by storing the integer 1 and the integer 3.

If you're comparing a result using doubles in Java and one in C, then you should read the Java spec and set up your C environment to use the same rounding modes and algorithms; the java.lang.Math trig functions are based on netlib rather than the C standard library.

Also remember ( what you should have been taught at school ) that if your inputs are only given to a certain number of significant figures, don't report the result to more significant figures than you have.

Pete Kirkham