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

Username:   Password: 
 RegisterRegister   
 Inequalities From Manipulating Real Value
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dragon20942




PostPosted: Sat Jan 24, 2015 4:55 pm   Post subject: Inequalities From Manipulating Real Value

Does anyone get a different output from mine? I posted mine at the bottom. Better yet, anyone have an explanation?

Turing:

var a : int := 0
var b : int := 3
var x : real := a

if x = a then %x is set = to a. This should output.
    put "x = a"
end if



for i : 1 .. 10 %modification of x. x becomes 3
    x += 0.3
end for



if x = b then %Comparing 3 with 3
    put "x = b"
end if



var c : real := 3 %Comparison of reals
if x = c then
    put "x = c"
end if



var d : real := 3.0 %Set to a real value
if x = d then
    put "x = d"
end if



var e : real := x %set equal to x
if x = e then
    put "x = e"
end if



if round (x) = b then %round x
    put "round(x) = b"
end if



var f : real := 0.3 %compare to similar process
f *= 10
if x = f then
    put "x = f"
end if



var g : real := 0 %compare to identical process
for i : 1 .. 10
    g += 0.3
end for
if x = g then
    put "x = g"
end if

/* OUTPUT
x = a
x = e
round(x) = b
x = g

So only works for setting equal, rounding the real, and identical process
*/



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Jan 24, 2015 6:12 pm   Post subject: RE:Inequalities From Manipulating Real Value

Real values are inaccurate due to the way they are stored in memory. Typically they are so many bits of data multiplied by some exponent. For example, the number 12340000 could be represented as 1234*10^4. If you only have 4 digits of precision, then 12343000 would also be represented as 1234*10^4. You lose the 3 because the Real cannot be precise enough to store it (Reals can store more than 4 digits ofc). Same for decimals, 1.234 is 1234*10^-3. 1.2345 is also 1234*10^-3. This is how reals work. Because we're using computers of course, this is all stored in binary. instead of 4 decimal digits of precision, we have so many bits (binary digits) of precision. Some numbers are very difficult to store in binary, like 0.1, which is more often stored as 0.999999999 or 1.000000001.

Can you see now why comparing reals to integers might not give you the answers you expect?
Dragon20942




PostPosted: Sat Jan 24, 2015 7:48 pm   Post subject: RE:Inequalities From Manipulating Real Value

Yes. Thank you so much! Now I just have to figure out how to work around it Smile
Tony




PostPosted: Sun Jan 25, 2015 3:17 am   Post subject: RE:Inequalities From Manipulating Real Value

Depends on what exactly you want to do.

If precision matters, use integers with smaller units. E.g. money is never implemented as floats -- it's all integers. Instead of $1.25 it's 125 cents (the actual unit might be a small fraction of a cent, if it matters).

Otherwise you check for how far away two floats are:
code:

abs(float_1 - float_2) < some_small_value
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: