Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2008, 10:28 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
Using double for money is a sin
Never use float or double for money or currencies. Use fixed point instead.
Here is a trivial program that shows you how you can not get floating point numbers to add properly.

Changing it from float to double changes where the error occurs, but its still wrong. Testing this is an exercise left to the student.

Code:
/* * Copyright (c) 2008, Pat Farrell All rights reserved. */ package com.pfarrell.utils.math; /** * The <code>FloatGrok</code> class implements sample code to show how floating point * numbers don't work the way you expect * * @author pfarrell */ public class FloatGrok { public FloatGrok() { } /** * do the test */ public void process() { float delta = 0.01F; float sum = 0; for (int i = 0; i < 100; i++) { float calc = ((float) i) * delta; if (sum != calc) { System.out.printf("i: %d sum: %f != calc: %f\n", i, sum, calc); break; } sum += delta; } } /** * usual shell program entry point * @param args the command line arguments */ public static void main(String[] args) { FloatGrok fg = new FloatGrok(); fg.process(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-15-2008, 03:29 AM
JDCAce's Avatar
Member
 
Join Date: Oct 2008
Location: Columbus, GA, US
Posts: 24
JDCAce is on a distinguished road
Send a message via AIM to JDCAce Send a message via MSN to JDCAce Send a message via Yahoo to JDCAce Send a message via Skype™ to JDCAce
I have seen some odd stuff with float and double, but I've never heard of fixed point.
__________________
"Things are made of littler things that jiggle."
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-15-2008, 03:46 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
It is well supported in Cobol, but no language that is cool has bothered with it.

Most Java or other languages that deal with this use an implicit fixed point. That is what you get when you use pennies for US currency.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-28-2008, 09:05 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
Here is a nice description of some of the issues. Its from a Python book, but that's fine, the issue is not Java or Python specific, its really a limitation of how float and double are implemented on modern computers.

An Introduction to Python - Floating Point Arithmetic Issues and Limitations
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Money Conversion - Proofreading pls? javanewbie New To Java 5 08-17-2008 03:40 PM
Java Developers - Want more money? Better Career prospects? Chance to be DV Cleared? MNoone Jobs Offered 1 08-09-2008 02:10 PM
Money to Words javanewbie New To Java 2 06-30-2008 06:27 AM
Calculating sin of a double value Java Tip Java Tips 0 01-13-2008 10:13 PM
transforming double to int AlejandroPenton New To Java 2 12-11-2007 03:34 AM


All times are GMT +3. The time now is 07:58 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org