Computer Science Canada

Decimals and Negatives

Author:  Crazy [ Sat Jan 17, 2009 9:46 pm ]
Post subject:  Decimals and Negatives

We have to make a calculator on turing I have everything else I am just having a little trouble with AC, decimal, and the negative part of the programming. The AC is working only for the fist number and only when I press an operator. The decimals and negatives are confusing. If someone can help that would be great!

Author:  DanielG [ Sat Jan 17, 2009 9:50 pm ]
Post subject:  RE:Decimals and Negatives

negative shouldn't be a problem, all you need is a boolean to check if negative, if it is, when adding a number add the negative of the number clicked, if not add the positive.

decimals is a bit more complex, you need a boolean variable (again) to store whether the dot has been placed yet or if it hasn't, you also need an integer depth (discussed later), if it hasn't you obviously multiply your current value by 10 then add the value, if it has been placed, then you have to divide the number by 10 as many times as the depth then add it to the number (without multiplying by 10)

hope this helps.

Author:  Crazy [ Sat Jan 17, 2009 10:27 pm ]
Post subject:  RE:Decimals and Negatives

that helps

can you give me an example of how this would look?for both the decimal and negative

Author:  DanielG [ Sat Jan 17, 2009 10:52 pm ]
Post subject:  RE:Decimals and Negatives

for negative, you have the number -100 stored, which you know is a negative, so when clicking on say 6 on the calculator, it would mutliply -100 by 10, giving -1000, then add -6, resulting in -1006, which is what should happen on a calculator.

for decimals, if '.' has not been pressed on, then do as above (only with positives assuming it is positive). if it has, then use the depth to see how much you divide by. for example, you have 1.31 stored, the depth would be 1000, since that is what you need to divide by, so when for example 3 is pressed, the number becomes 1.31+3/1000, afterwards, remember to multiply depth by 10 after opearation (earler I said take 10^depth, but this is easier)

Author:  Crazy [ Sat Jan 17, 2009 11:00 pm ]
Post subject:  RE:Decimals and Negatives

thank you it actually helps
lol
I understand somewat

Author:  DanielG [ Sat Jan 17, 2009 11:11 pm ]
Post subject:  RE:Decimals and Negatives

np

Author:  Crazy [ Sat Jan 17, 2009 11:26 pm ]
Post subject:  Re: Decimals and Negatives

Quote:
if op= "4" then %negative
drawfillbox (100, 200, 350, 365, black)
num1 *10+num2
end if


i made the negative sign into op 4 the drawfillbox is my screen

does that look right?

I'm sory if im bothering you but imnew to turing and we didnt learn alot in class.

Author:  DanielG [ Sat Jan 17, 2009 11:42 pm ]
Post subject:  RE:Decimals and Negatives

wait, can you properly explain what the assignment is? I want to be sure I haven't explained to you something that is completely unrelated.

Author:  Crazy [ Sat Jan 17, 2009 11:45 pm ]
Post subject:  RE:Decimals and Negatives

I think you have explained it properly I'm not getting it. We have to make a calculator the basic functions +,-,*, and /. WIth those functions I am also including negatives, decimals, and an ac button.

Author:  DanielG [ Sat Jan 17, 2009 11:50 pm ]
Post subject:  RE:Decimals and Negatives

how do you get the input? what is ac again?

Author:  Crazy [ Sat Jan 17, 2009 11:55 pm ]
Post subject:  RE:Decimals and Negatives

we used the buttonwait so the users click on the numbers and the show on the screen.

AC- all clear like we see on calculators

This is how im doing the operations...

Turing:
if op = "1" then                         %+
   drawfillbox (75, 390, 350, 365, black)
    Font.Draw (intstr (-num1 + num2), 345 - 10 * length (intstr (num1 + num2)), 370, font1, white)

elsif op = "2" then                     %-
    drawfillbox (75, 390, 350, 365, black)     
    Font.Draw (intstr (num1 - num2), 345 - 10 * length (intstr (num1 - num2)), 370, font1, white)

elsif op = "3" then                     %(*)
    drawfillbox (75, 390, 350, 365, black)     
    Font.Draw (intstr (num1 * num2), 345 - 10 * length (intstr (num1 * num2)), 370, font1, white)

elsif op = "4" then                     %(/)
    drawfillbox (75, 390, 350, 365, black)     
    Font.Draw (intstr (floor (num1 / num2)), 345 - 10 * length (intstr (floor (num1 / num2))), 370, font1, white)


Not sure about this one
Turing:
elsif op = "5"  then %negative (-)
  drawfillbox (75, 390, 350, 365, black)   
 Font.Draw (intstr (-num1)  (-num2 ), 345 - 10 * length (intstr  (-num1) (-num2)), 370, font1, white)



Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]

Author:  Crazy [ Sun Jan 18, 2009 8:09 pm ]
Post subject:  RE:Decimals and Negatives

lol i got the negative to work thanks for ur help and the users on chat Very Happy

So for the decimals as u said before instead of multiplying it by 10 we would divde number by 10?

Author:  deltatux [ Sun Jan 18, 2009 8:32 pm ]
Post subject:  RE:Decimals and Negatives

I don't remember Turing too much, but if you can set it to a double (haven't used Turing since I ditched it in grade 9 ... or 4 yrs ago) you can store decimal numbers.

in Java, I would do something like this:

code:

double dblDecimal = 0;


I think it's called real numbers in Turing.

deltatux

Author:  Crazy [ Sun Jan 18, 2009 8:38 pm ]
Post subject:  RE:Decimals and Negatives

ye it is real numbers, i dont know where to go on form there

Author:  DanielG [ Sun Jan 18, 2009 8:41 pm ]
Post subject:  RE:Decimals and Negatives

you need to store how many numbers after the decimal point you have placed, and divide by a power of 10 accordingly, or even better, whenever adding a number, multiply your division constant by 10.

Author:  deltatux [ Sun Jan 18, 2009 8:42 pm ]
Post subject:  RE:Decimals and Negatives

then couldn't you just store them in that data type? It works just like an int except with no negatives...

deltatux

Author:  Crazy [ Sun Jan 18, 2009 10:32 pm ]
Post subject:  Re: Decimals and Negatives

so do i have to make it into a seperate var


var decimal: real


how would i store the numbers into to decimals?

Author:  DanielG [ Sun Jan 18, 2009 10:45 pm ]
Post subject:  RE:Decimals and Negatives

if you use real instead of integers then you don't have to worry about ways to store the number, all you have to make sure to do is to divide/multiply such that the number added is small enough

Author:  Crazy [ Sun Jan 18, 2009 11:08 pm ]
Post subject:  Re: Decimals and Negatives

thats my code for the operators to that Inculde * or /

Turing:
if op = "1" then                         %+
    drawfillbox (75, 390, 350, 365, black)
    Font.Draw (intstr (num1 + num2), 345 - 12 * length (intstr (num1 + num2)), 370, font1, white)

elsif op = "2" then                     %-
    drawfillbox (75, 390, 350, 365, black)
    Font.Draw (intstr (num1 - num2), 345 - 12 * length (intstr (num1 - num2)), 370, font1, white)

elsif op = "3" then                     %(*)
    drawfillbox (75, 390, 350, 365, black)
    Font.Draw (intstr (num1 * num2), 345 - 12 * length (intstr (num1 * num2)), 370, font1, white)

elsif op = "4" then                     %(/)
    drawfillbox (75, 390, 350, 365, black)
    Font.Draw (intstr (floor (num1 / num2)), 345 - 12 * length (intstr (floor (num1 / num2))), 370, font1, white)

end if

Author:  DanielG [ Mon Jan 19, 2009 7:50 am ]
Post subject:  RE:Decimals and Negatives

don't use floor for divison, as it rounds the number and you don't want that, use realstr for the second part so it converts the real to string

Author:  Crazy [ Mon Jan 19, 2009 10:22 pm ]
Post subject:  RE:Decimals and Negatives

oh ok
Thank you Smile


: