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

Username:   Password: 
 RegisterRegister   
 Adding Increment of 0.1
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Senecide




PostPosted: Mon Feb 28, 2011 12:34 am   Post subject: Adding Increment of 0.1

What is it you are trying to achieve?
I am new to Turing and have been trying to learn it for the past couple of months. Currently I am trying to write a program that continuously adds increments of 0.1 to variable "x" and exits when "x" reaches a specific number.


What is the problem you are having?
The problem is that Turing continues to loop the program even though "x" reached the specific number. It continues adding the increment to "x". But when I decide to change the increment to 1, the program stops successfully. I am still getting used to how Turing behaves, and I would appreciate it if someone explains to me why this happens.


Describe what you have tried to solve this problem
I tried looking through the problem with the built-in debugger and it looks like Turing goes over the exit code but doesn't actually exit.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:

var x : real := -5
const incriment := 0.1
for i : 1 .. 100
    put x
    x := x + incriment
    exit when x = 0
end for


Please specify what version of Turing you are using
I am using Turing 4.0.5
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Feb 28, 2011 1:42 am   Post subject: RE:Adding Increment of 0.1

When dealing with floating points (reals), = operator is not guaranteed to work.

Remember: integers are for counting things, while floats (reals) are for measuring things.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Senecide




PostPosted: Mon Feb 28, 2011 2:02 am   Post subject: Re: Adding Increment of 0.1

Interesting, so it would seem like it's impractical to use real in this situation. But what if for some reason I want a program to stop when x counts to 0.5? How can this work if x can't be a real number?
Tony




PostPosted: Mon Feb 28, 2011 2:29 am   Post subject: RE:Adding Increment of 0.1

you could count in integers and do it 10 times as much _or_ you could check if the measurement is "close enough"

e.g.
code:

exit when x >= -0.01
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Senecide




PostPosted: Mon Feb 28, 2011 5:57 pm   Post subject: Re: Adding Increment of 0.1

Interesting, because my program now stops when I add in "exit when x >= 0". The reason I am trying to trying to make a program that adds 0.1 to "x" is because I was reading one of the Turing tutorials called "Turing as a Functional Programming Language" by Cervantes, where he found the root of the function f(x) = x^2 - 4 by adding 0.1 into "x" until f(x) = 0
http://compsci.ca/v3/viewtopic.php?t=12376

I thought I could take the same concept and see if I can make a program to find the roots of other functions. But now I am not sure anymore. Can somebody explain to me how the program Cervantes made worked? Or is this stuff way over my head? I hope I am not going too off topic.
DemonWasp




PostPosted: Mon Feb 28, 2011 7:15 pm   Post subject: RE:Adding Increment of 0.1

You may notice that he uses round to convert a real number to an integer, so he can use the equals operation. He even leaves a comment explaining that "The rounding here is to avoid things like 0.000000000023345 not equalling 0".
Senecide




PostPosted: Mon Feb 28, 2011 8:17 pm   Post subject: Re: Adding Increment of 0.1

Oh! Thanks! I never quite understand why Turing would display stuff like 0.000000000023345. I also realized that the program divides the equation by the increment (which is 0.1) and when it doesn't, it displays the wrong answer. Does this also have to do with using the equals operation? Or does it do something else?
Tony




PostPosted: Mon Feb 28, 2011 8:59 pm   Post subject: RE:Adding Increment of 0.1

It isn't Turing, but your CPU's implementation of floating points. Read up on the standard: http://en.wikipedia.org/wiki/IEEE_754-2008

The important part is that floating point values are represented by
code:

(-1)^s * c * b^q

This lets one represent a lot of different numbers at a lot of different ranges, but at the cost of precision (which is ok, because we have integers for where precision matters).

so, for example

0.1 ~= (-1)^0 * 100 * 2^-10 = 0.09765625

certain numbers are just not easily represented as powers of 2s.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Feb 28, 2011 11:37 pm   Post subject: RE:Adding Increment of 0.1

In the same way that 1/3 doesn't represent well in fixed-width decimal (0.3333333...), most floating-point numbers don't represent particularly well in fixed-width binary (0.0110110...). You end up with a number that is close to, but not quite exactly, the one that you were hoping for. The floating-point standard is actually very very good at representing floating point numbers, it just can't be precise (nothing can, really).
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  [ 9 Posts ]
Jump to:   


Style:  
Search: