Page 1 of 1

Weird Maths

Posted: Tue Sep 08, 2015 8:56 pm
by nblackburn
In PureBasic i am trying to do some calculations to determine the height of something but for some reason the following returns zero, any thoughts?

Calculation:

Code: Select all

(100 / 498) * 491 = 98.59437751
PureBasic:

Code: Select all

Debug (100 / 498) * 491 = 0
This is very strange to me and am not sure what is going wrong :?

Re: Weird Maths

Posted: Tue Sep 08, 2015 8:58 pm
by Keya
I think you just have to tell PB that its a floating point value, because if you encase the equation in StrF() it works. I had that problem too before I discovered StrF() and was just using Str() heehee :)

Re: Weird Maths

Posted: Tue Sep 08, 2015 9:00 pm
by nblackburn
Thanks, this was really off putting :)

Re: Weird Maths

Posted: Tue Sep 08, 2015 9:00 pm
by Shardik
Just try the following:

Code: Select all

Debug StrD((100 / 498) * 491, 8) ; Output: 98.59437751

Re: Weird Maths

Posted: Tue Sep 08, 2015 9:02 pm
by Keya
... yes or Doubles using StrD! ........ whichever one... Floats your boat :oops: :D [edit] ok sorry that was terrible

Re: Weird Maths

Posted: Tue Sep 08, 2015 9:29 pm
by GPI
When you want to use float/double, you should add a ".0". Otherwise the compiler think you want to use integer.

Debug (100.0 / 498.0) * 491.0

Btw. When you want to use only Integer, better first * than /
debug (100*491)/498