Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick question on rounding :-)
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




PostPosted: Tue Oct 04, 2005 3:47 pm   Post subject: (No subject)

code:
put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false

I don't get it... It IS false, since one of the numbers is 0.1, and the other is 0.01. So what's so special about this?
Sponsor
Sponsor
Sponsor
sponsor
beard0




PostPosted: Tue Oct 04, 2005 3:50 pm   Post subject: (No subject)

[Gandalf] wrote:
code:
put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false

I don't get it... It IS false, since one of the numbers is 0.1, and the other is 0.01. So what's so special about this?


You know what he meant... or at least, if you read the rest of the topic you do.
wtd




PostPosted: Tue Oct 04, 2005 4:39 pm   Post subject: (No subject)

rizzix wrote:
No not all languages... Languages like Haskell should not have such a problem.. In haskell the floating points are represented differently.


Are you sure? Smile

code:
insaneones@ubuntu:~ $ ghci
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.2.2, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
Prelude> let (x, y, z) = (5.0, 0.01, 0.02)
Prelude> x + y + z == z + y + x
False
Prelude> :q
Leaving GHCi.
insaneones@ubuntu:~ $ hugs
__   __ __  __  ____   ___      _________________________________________
||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-2003
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Report bugs to: hugs-bugs@haskell.org
||   || Version: November 2003  _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions

Type :? for help
Prelude> let (x, y, z) = (5.0, 0.01, 0.02) in x + y + z == z + y + x
False
Prelude> :q
[Leaving Hugs]
insaneones@ubuntu:~ $
rizzix




PostPosted: Tue Oct 04, 2005 9:13 pm   Post subject: (No subject)

ooh. guess not.. i just typed in the number (didn't use any variables) seemed to have printed out True. Wow, so unpredictable.. hmm
wtd




PostPosted: Tue Oct 04, 2005 9:22 pm   Post subject: (No subject)

code:
$ ledit mit-scheme
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright 2004 Massachusetts Institute of Technology.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Tuesday October 19, 2004 at 6:00:49 AM
  Release 7.7.90 || Microcode 14.11 || Runtime 15.3 || SF 4.41 || LIAR 4.116
  Edwin 3.114

1 ]=> (let ((x 5.0 )
            (y 0.02)
            (z 0.01))
         (if (= (+ x y z) (+ y z x))
            "foo"
            "bar"))
;Value 11: "bar"

1 ]=>
beard0




PostPosted: Tue Oct 04, 2005 9:37 pm   Post subject: (No subject)

Hooray for:
Mathematica:
x = 5.0;
y = 0.02;
z = 0.01;
If[x + y + z == y + z + x, "True", "False"]
(*This returns "True"!!!!*)

N.B. The absence of a ; on the last line is intentional. In mathematica, no end of line chacter is necessary, the semi-colon simply serves to supress output from the last statement. Had I placed a semi-colon on the end of the last line, I would have had no ouput.
wtd




PostPosted: Fri Oct 07, 2005 5:24 pm   Post subject: (No subject)

O'Caml:

code:
# let x = 3.0 and y = 0.02 and z = 0.01 in
   x +. y +. z = y +. x +. z;;
- : bool = true
wtd




PostPosted: Fri Oct 07, 2005 7:26 pm   Post subject: (No subject)

SML/NJ:

code:
- open Real;
- let val x = 3.0
=     val y = 0.03
=     val z = 0.01
= in
=     Real.==(x + y + z, y + x + z)
= end;
val it = true : bool
Sponsor
Sponsor
Sponsor
sponsor
xHoly-Divinity




PostPosted: Wed Nov 30, 2005 5:47 pm   Post subject: (No subject)

I think that multiplying by 100, rounding, then dividing by 100 is the fastest way to do it...
MysticVegeta




PostPosted: Wed Nov 30, 2005 6:02 pm   Post subject: (No subject)

Did you check the date on the topic?
xHoly-Divinity




PostPosted: Tue Jan 17, 2006 5:11 pm   Post subject: (No subject)

MysticVegeta wrote:
Did you check the date on the topic?


Why?
beard0




PostPosted: Tue Jan 17, 2006 5:17 pm   Post subject: (No subject)

xHoly-Divinity wrote:
MysticVegeta wrote:
Did you check the date on the topic?


Why?


FFS, because you're necro-posting, bringing up old topics - this is not appreciated, and you have just done it again. If the topic is old, let it die. Read it if you're interested, and if you feel an overwelming urge to share some revelation that a certain post has brought to you, PM the poster, and leave the old topic be.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 27 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: