Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  confusing double behavior  (Read 1090 times)
0 Members and 1 Guest are viewing this topic.
Offline f.l.x

Senior Devvie


Projects: 3


there is no place like 127.0.0.1


« Posted 2005-12-02 11:55:20 »

This code:
1  
2  
3  
4  
5  
6  
7  
8  
double d = 0;
for (int i = 0; i < 100; i++) {

   d += 0.001 * i;

   System.out.println(d);

}


should output exact double values betwen 0.0 and 4.95.

but it gives me unexact values like:
0.0
0.0010
0.0030
0.0060
0.01
0.015
0.020999999999999998
0.027999999999999997
[...]
4.753000000000001
4.851000000000001
4.950000000000001

any ideas of why double addition is behaving in such an inexact way?

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2005-12-02 12:17:57 »

I had this very same question.

Some commonly used values, like 0.1, cannot be exactly expressed in floating point notation.
It sucks, but there you go.
Offline f.l.x

Senior Devvie


Projects: 3


there is no place like 127.0.0.1


« Reply #2 - Posted 2005-12-02 14:07:02 »

then i'll have to "round" values like 0.6 to "0.6" for a simple addition!?

that¡s great ¬¬

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #3 - Posted 2005-12-02 19:27:07 »

It doesn't suck. Floating point numbers are not supposed to be used for exact calculations! This is the first thing anyone should know about them.

The reason is that internally computers work with the binary system. A fraction expressible with finitely many decimals in the decimal system may not be thus expressible in binary. You have the same problem in the decimal system.

If you want exact calculations, you will have to write (or borrow) some library which deals with rational numbers. Just converting to integers generally won't help you, since you will still suffer from infinitely repeating fractions now and then.

By the way, floating point numbers are extremely powerful. Integers would use 32 bits to express all mathematical integers from minus two billion to plus two billion, which is fine. A floating point number has two parts:

1) the mantissa (say, M), which generally uses most of the bits, which expresses decimals purely.
2) the exponent (let's call it q), which determines how large the value actually is.

A number is expressed by

M * 2^q,

and thanks to this notation, floating point numbers can be exceedingly large. With 32 bits distributed meaningfully between mantissa and exponent, you can express numbers as large as 10^38, though many adjacent mathematical integers in that range have the same floating point representation. With 64 bits you can reach something like 10^308.

(edit: minor corrections)
Offline f.l.x

Senior Devvie


Projects: 3


there is no place like 127.0.0.1


« Reply #4 - Posted 2005-12-02 23:51:32 »

yeah i learned how to read and operate with numbers on fp on 1st year, but i've never faced this problem before. it is acurate anyway, so it is only a problem for displaying information.

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #5 - Posted 2005-12-03 03:59:37 »

It is not accurate. Use integers for any exact calculations.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #6 - Posted 2005-12-03 05:08:45 »

Look up these classes...

1  
2  
NumberFormat
NumberFomatter


If you don't specify the precision that you wish to use you end up seeing the full representation of how close the floating point number can come to the true value.  Look at the significant digits and you can see that you probably don't care in most cases that it is off by such a tiny amount - it is only the display that is a bother.  So when the display matters, use a NumberFomatter or the newer Java 5 Formatting stuff to (printf even) to display the number.

Offline f.l.x

Senior Devvie


Projects: 3


there is no place like 127.0.0.1


« Reply #7 - Posted 2005-12-03 12:03:36 »

It is not accurate. Use integers for any exact calculations.

acurate enought for a zoom factor, from 0.01 to 2.00

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (82 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!