Weird Maths

Just starting out? Need help? Post your questions and find answers here.
User avatar
nblackburn
User
User
Posts: 67
Joined: Mon Aug 19, 2013 1:22 am
Location: United Kingdom
Contact:

Weird Maths

Post 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 :?
Image
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Weird Maths

Post 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 :)
User avatar
nblackburn
User
User
Posts: 67
Joined: Mon Aug 19, 2013 1:22 am
Location: United Kingdom
Contact:

Re: Weird Maths

Post by nblackburn »

Thanks, this was really off putting :)
Image
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Weird Maths

Post by Shardik »

Just try the following:

Code: Select all

Debug StrD((100 / 498) * 491, 8) ; Output: 98.59437751
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Weird Maths

Post by Keya »

... yes or Doubles using StrD! ........ whichever one... Floats your boat :oops: :D [edit] ok sorry that was terrible
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Weird Maths

Post 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
Post Reply