[Help] Real value issue
Author |
Message |
tjmoore1993
|
Posted: Fri Apr 03, 2009 12:13 pm Post subject: [Help] Real value issue |
|
|
Hello, I came across this problem while writting a game in Turing. The problem is not that big of an issue but it causes problems where my "REAL" value is too large because of the math I am using. I am trying to figure out how to limit the numbers after the decimal to only 2 numbers so it displays like this.
%6.39
not
%6.3333333333339
If anyone could be of an assistance that would be great.
NOTE : None of the help given on this will be handed in for class marks. All help given will be studied
till understood and will be rewritten to make sense to me. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Fri Apr 03, 2009 12:39 pm Post subject: RE:[Help] Real value issue |
|
|
This is done by modifying not the value in the variable, but the way you're outputting it.
Specifically, look in the Turing help for the put command. In that entry, look at what a putItem can be:
code: |
The general form of a putItem is one of:
(a) expn [:widthExpn [:fractionWidth [:exponentWidth ] ] ]
(b) skip
|
Well, part B is useless to us. Part A is more useful. So far, you've probably been using put realVar, which is just the "expr" part of A. What you really want to do is something like put realVar : * : 2 which includes the ":widthExpn" and ":fractionWidth" parts of A. This tells Turing that you want 4 spaces for the output of realVar and 2 decimal places.
Edit: removed unneeded backslashes. |
|
|
|
|
|
tjmoore1993
|
Posted: Fri Apr 03, 2009 1:13 pm Post subject: RE:[Help] Real value issue |
|
|
I've got it to work but only when I use
put char_calc:2:2
The problem is, is that I am currently using this
Font.Draw (realstr (char_calc, 1), 419, 22, exp_text, white)
to display my variable. |
|
|
|
|
|
Insectoid
|
Posted: Fri Apr 03, 2009 2:24 pm Post subject: RE:[Help] Real value issue |
|
|
I would use math. Multiple it by 10, so 3.141592654 would be 314.1292654, then cut off the decimal using the div command. (div does division but cuts off the remainder)
which gives 314. Then divide by 10. Essentially,
code: |
put 3.141592654*10 div 1 / 10
|
Or, in longer code,
code: |
var pi := 3.141592654
pi := pi * 10
pi := pi div 1
pi := pi / 10
put pi
|
Alternatively, you can use the round() function instead of div. Still have to multiply/divide by 10. |
|
|
|
|
|
tjmoore1993
|
Posted: Fri Apr 03, 2009 2:34 pm Post subject: Re: RE:[Help] Real value issue |
|
|
insectoid @ Fri Apr 03, 2009 2:24 pm wrote: I would use math. Multiple it by 10, so 3.141592654 would be 314.1292654, then cut off the decimal using the div command. (div does division but cuts off the remainder)
which gives 314. Then divide by 10. Essentially,
code: |
put 3.141592654*10 div 1 / 10
|
Or, in longer code,
code: |
var pi := 3.141592654
pi := pi * 10
pi := pi div 1
pi := pi / 10
put pi
|
Alternatively, you can use the round() function instead of div. Still have to multiply/divide by 10.
This works instead of 10 though I must use 100 to get a Decimal to the 100th or w/e |
|
|
|
|
|
Insectoid
|
Posted: Fri Apr 03, 2009 2:43 pm Post subject: RE:[Help] Real value issue |
|
|
Ah...yes...er...100. Multiply and divide by 100...That's what I meant... |
|
|
|
|
|
Dusk Eagle
|
Posted: Fri Apr 03, 2009 2:49 pm Post subject: Re: [Help] Real value issue |
|
|
To add on to what Insectoid said, you could set a value for the number of decimal places you want:
Turing: |
const DEC_PLACES : int := 3
|
You then simply multiply/divide by 10**DEC_PLACES [Note: ** indicates exponents] |
|
|
|
|
|
blankout
|
Posted: Fri Apr 03, 2009 3:45 pm Post subject: Re: [Help] Real value issue |
|
|
tjmoore1993 @ Fri Apr 03, 2009 12:13 pm wrote: Hello, I came across this problem while writting a game in Turing. The problem is not that big of an issue but it causes problems where my "REAL" value is too large because of the math I am using. I am trying to figure out how to limit the numbers after the decimal to only 2 numbers so it displays like this.
%6.39
not
%6.3333333333339
If anyone could be of an assistance that would be great.
NOTE : None of the help given on this will be handed in for class marks. All help given will be studied
till understood and will be rewritten to make sense to me.
Simplest thing is after the equation put :1:2
for example,
put 1+2:1:2 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
tjmoore1993
|
Posted: Fri Apr 03, 2009 4:25 pm Post subject: Re: [Help] Real value issue |
|
|
THANKS |
|
|
|
|
|
|
|