Hopefully a quick and easy maths problem...

BlitzMax Forums/BlitzMax Beginners Area/Hopefully a quick and easy maths problem...

bubbz(Posted 2005) [#1]
In this code:
[CODE]
Graphics 800,600,0
Global blah:Float
blah = 1/2
DrawText (blah,10,10)
Flip
Repeat
Until KeyHit(KEY_ESCAPE)
[/CODE]

By my reasoning 'blah' should be equal to 0.5, so why does the value appear to be 0 ?

I expect i'm missing something REALLY obvious :-\


Oddball(Posted 2005) [#2]
Graphics 800,600,0
Global blah:Float
blah = 1.0/2.0
DrawText (blah,10,10)
Flip
Repeat
Until KeyHit(KEY_ESCAPE)
Solved.


bubbz(Posted 2005) [#3]
Thanks.... but.... In my original code i'm actually dividing two elements from an array, e.g:

blah = (myarray[0] - myarray[x])/myarray[0]

(i need to find the difference between the current (x) value of the array and the first (0) value as a percentage of the first value)

So is this still so easily solved? and how do I indicate code in the text (since [CODE][/CODE] obviously isn't right ;)


Oddball(Posted 2005) [#4]
blah = (Double(myarray[0]) - Double(myarray[x]))/Double(myarray[0])
Solved again. Oh and forum codes are all lower case. See What are the forum codes? for more codes.


Oddball(Posted 2005) [#5]
Also see the Blitz Max help/language/types/type balancing for an explanation on what is happening in that equation. In the previous equation you could have got away with only casting one arguement.
blah = (Double(myarray[0]) - myarray[x])/myarray[0]
But it's safer just to cast them all.


bubbz(Posted 2005) [#6]
Works like a dream, many thanks for your rapid help when just when i needed it :)

I'm assuming from this that calculating floats using integer values will give an integer result? Seems strange behaviour to my inexperienced mind, is there a reason for it?


bubbz(Posted 2005) [#7]
I only saw your last post after I'd posted my reply to the previous one. Thanks for pointing me to the correct part of the help file and bearing with me as I stumble on step further up the blitz learning curve :)