You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Gunna Satria <gu...@yahoo.com> on 2006/01/22 00:42:03 UTC

Re: [OT] 3.163 + 0.001 = ??? (found solution, i think)

Hi All,
  Thanks for the replies...i really appreciate it
  I already tried using BigDecimal.. and still the result is not like i expected.
BigDecimal a = new BigDecimal(3.163);
  BigDecimal b = new BigDecimal(0.001);
  BigDecimal c = a.add(b);
  System.out.println("c = "+c);
  System.out.println("c db = "+c.doubleValue());
  and the result is,
    c = 3.163999999999999811726991705285172429285012185573577880859375
  c db = 3.1639999999999997
  finally i found a workaround for this..
  i use,
  double a = 3.163;
  double b = 0.001;
  double c = a+b;
  c = Math.round(c*1000);
  c = c/1000;
  double d = (Math.round(c*1000))/1000;
  System.out.println("c = "+c);
  System.out.println("d = "+d);
  here the result for c is correct, 3.164
  but.... you all may guess that d will yield same result as c, but...
  you guys are wrong!!!!
  d is 3.0
  i have no comment on this.. maybe you guys knew something on this.
  well... atleast i found a workaround using the round thing,
  but..
  that means that i have to change my whole program.... oo gosh..
  fyi, i'am running the program under sun solaris 8.
  this is only tip of the iceburg,
  i'm doing this calculation for gas splitting..
  so it involves a lof of money...
  so you guys can imagine that 3.163 and different of 0.001 is important
  for example,
  if production = 30000 barrel
  0.001 * 30000(daily produced oil) * 67 US$ * 30 days = 60300 US$
  and 30000 is only 10% from total production.
  But again, thanks for let me sharing the pain :D
   
  best regards,
   
  Gunna


		
---------------------------------
Yahoo! Photos
 Ring in the New Year with Photo Calendars. Add photos, events, holidays, whatever.

Re: [OT] 3.163 + 0.001 = ??? (found solution, i think)

Posted by Sergei Dubov <sd...@gmail.com>.
Dude, you really need to pass a String to BigDecimal, not a double, a 
double is still a double.

By the way, this is a Tapestry mailing list... I recommend that you get 
"Java Puzzlers" book written by the real gurus.

http://www.amazon.com/gp/product/032133678X/qid=1137887465/sr=2-1/ref=pd_bbs_b_2_1/002-2850119-9397667?s=books&v=glance&n=283155

This will get you the answers on esoteric questions. gee.

-S.





Gunna Satria wrote:
> Hi All,
>   Thanks for the replies...i really appreciate it
>   I already tried using BigDecimal.. and still the result is not like i expected.
> BigDecimal a = new BigDecimal(3.163);
>   BigDecimal b = new BigDecimal(0.001);
>   BigDecimal c = a.add(b);
>   System.out.println("c = "+c);
>   System.out.println("c db = "+c.doubleValue());
>   and the result is,
>     c = 3.163999999999999811726991705285172429285012185573577880859375
>   c db = 3.1639999999999997
>   finally i found a workaround for this..
>   i use,
>   double a = 3.163;
>   double b = 0.001;
>   double c = a+b;
>   c = Math.round(c*1000);
>   c = c/1000;
>   double d = (Math.round(c*1000))/1000;
>   System.out.println("c = "+c);
>   System.out.println("d = "+d);
>   here the result for c is correct, 3.164
>   but.... you all may guess that d will yield same result as c, but...
>   you guys are wrong!!!!
>   d is 3.0
>   i have no comment on this.. maybe you guys knew something on this.
>   well... atleast i found a workaround using the round thing,
>   but..
>   that means that i have to change my whole program.... oo gosh..
>   fyi, i'am running the program under sun solaris 8.
>   this is only tip of the iceburg,
>   i'm doing this calculation for gas splitting..
>   so it involves a lof of money...
>   so you guys can imagine that 3.163 and different of 0.001 is important
>   for example,
>   if production = 30000 barrel
>   0.001 * 30000(daily produced oil) * 67 US$ * 30 days = 60300 US$
>   and 30000 is only 10% from total production.
>   But again, thanks for let me sharing the pain :D
>    
>   best regards,
>    
>   Gunna
> 
> 
> 		
> ---------------------------------
> Yahoo! Photos
>  Ring in the New Year with Photo Calendars. Add photos, events, holidays, whatever.

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: [OT] 3.163 + 0.001 = ??? (finish)

Posted by Gunna Satria <gu...@yahoo.com>.
Okay,
  Thanks for the all solution.
  Yes, sorry for the others, for i had make your inbox full with my stupidity.
   
  best regards,
   
  Gunna

Patrick Casey <pa...@adelphia.net> wrote:
  
The problem is where you're setting the bigdecimal.

BigDecimal a = new BigDecimal(3.163) sets the bigdecimal to the
*double* 3.163. The double is the implied type of any constant set in java
if there isn't enough information available to type it explicitely.

To set the BigDecimal to precisely 3.163, use:

BigDecimal a = new BigDecimal("3.163");
[deleted]
		
---------------------------------
 
 What are the most popular cars?  Find out at Yahoo! Autos

RE: [OT] 3.163 + 0.001 = ??? (finish)

Posted by Gunna Satria <gu...@yahoo.com>.
Okay,
  Thanks for the all solution.
  Yes, sorry for the others, for i had make your inbox full with my stupidity.
   
  best regards,
   
  Gunna

Patrick Casey <pa...@adelphia.net> wrote:
  
The problem is where you're setting the bigdecimal.

BigDecimal a = new BigDecimal(3.163) sets the bigdecimal to the
*double* 3.163. The double is the implied type of any constant set in java
if there isn't enough information available to type it explicitely.

To set the BigDecimal to precisely 3.163, use:

BigDecimal a = new BigDecimal("3.163");
[deleted]
		
---------------------------------
 
 What are the most popular cars?  Find out at Yahoo! Autos

RE: [OT] 3.163 + 0.001 = ??? (found solution, i think)

Posted by Patrick Casey <pa...@adelphia.net>.
	The problem is where you're setting the bigdecimal.

	BigDecimal  a = new BigDecimal(3.163) sets the bigdecimal to the
*double* 3.163. The double is the implied type of any constant set in java
if there isn't enough information available to type it explicitely.

	To set the BigDecimal to precisely 3.163, use:

	BigDecimal a = new BigDecimal("3.163");

	I have to agree a bit with Sergai here; this is pretty basic java
blocking and tacking stuff that doesn't have a lot of relevance to Tapestry.
Additionally, and I can't find a nicer way to say this so sorry about the
way this is going to come out, but if you're seriously lost and confused by
things like the nature of floating point numbers, then you'll definitely
benefit from some time spent on basic java programming before you dive into
a web development framework.

	--- Pat

> -----Original Message-----
> From: Gunna Satria [mailto:gunna_satria@yahoo.com]
> Sent: Saturday, January 21, 2006 3:42 PM
> To: Tapestry users
> Subject: Re: [OT] 3.163 + 0.001 = ??? (found solution, i think)
> 
> Hi All,
>   Thanks for the replies...i really appreciate it
>   I already tried using BigDecimal.. and still the result is not like i
> expected.
> BigDecimal a = new BigDecimal(3.163);
>   BigDecimal b = new BigDecimal(0.001);
>   BigDecimal c = a.add(b);
>   System.out.println("c = "+c);
>   System.out.println("c db = "+c.doubleValue());
>   and the result is,
>     c = 3.163999999999999811726991705285172429285012185573577880859375
>   c db = 3.1639999999999997
>   finally i found a workaround for this..
>   i use,
>   double a = 3.163;
>   double b = 0.001;
>   double c = a+b;
>   c = Math.round(c*1000);
>   c = c/1000;
>   double d = (Math.round(c*1000))/1000;
>   System.out.println("c = "+c);
>   System.out.println("d = "+d);
>   here the result for c is correct, 3.164
>   but.... you all may guess that d will yield same result as c, but...
>   you guys are wrong!!!!
>   d is 3.0
>   i have no comment on this.. maybe you guys knew something on this.
>   well... atleast i found a workaround using the round thing,
>   but..
>   that means that i have to change my whole program.... oo gosh..
>   fyi, i'am running the program under sun solaris 8.
>   this is only tip of the iceburg,
>   i'm doing this calculation for gas splitting..
>   so it involves a lof of money...
>   so you guys can imagine that 3.163 and different of 0.001 is important
>   for example,
>   if production = 30000 barrel
>   0.001 * 30000(daily produced oil) * 67 US$ * 30 days = 60300 US$
>   and 30000 is only 10% from total production.
>   But again, thanks for let me sharing the pain :D
> 
>   best regards,
> 
>   Gunna
> 
> 
> 
> ---------------------------------
> Yahoo! Photos
>  Ring in the New Year with Photo Calendars. Add photos, events, holidays,
> whatever.



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org