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

Username:   Password: 
 RegisterRegister   
 comparing double to integer
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Sun Aug 13, 2006 2:18 pm   Post subject: comparing double to integer

how would i compare a number that is declared as a double although it is an integer that just has a zero at the end. ex: 123.0 to an integer. i can't declare the double number as an integer because i'm using Math.Pow and it always makes it double, but i wanna take the answer from that and see if it equals to an integer number. for example does 123.0 = 123. this will not be equal, thus i will not get any results. any suggestions?
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Sun Aug 13, 2006 2:49 pm   Post subject: (No subject)

Have you tried: instead of casting the Math.Pow variable cast the integer you are comparing to a float? Just a suggestion.

(Math.Pow.float_int == (float) int_variable)
Justin_




PostPosted: Sun Aug 13, 2006 2:52 pm   Post subject: (No subject)

Or wait, I misunderstood. Just cast the double returned from Math.Pow and compare...

if ((int) power_returned == int_)
wtd




PostPosted: Sun Aug 13, 2006 2:56 pm   Post subject: (No subject)

Keep in mind that floating point numbers are inherently imprecise. Thus it can be difficult to directly compare two such numbers for equality.
r.3volved




PostPosted: Sun Aug 13, 2006 4:47 pm   Post subject: (No subject)

also, casting a double to an int will either truncate the decimals or round them...

You would have to cast your integer to a double to compare the two, not visa versa
bugzpodder




PostPosted: Sat Sep 16, 2006 8:12 pm   Post subject: (No subject)

add some epsilon constant to the result and truncate it by casting to an int
McKenzie




PostPosted: Tue Sep 19, 2006 11:54 am   Post subject: (No subject)

Floating point numbers are imprecise only when representing fractions, or extreamely large numbers. Casting it should be fine.
bugzpodder




PostPosted: Thu Sep 21, 2006 7:06 pm   Post subject: (No subject)

http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=integersReals
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Thu Sep 21, 2006 7:11 pm   Post subject: (No subject)

cool! i forgot all about x&(x-1) !! now i can use it to improve my AI Smile
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 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: