This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Maths formula, what?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 03-05-2015 , 21:06   Maths formula, what?
Reply With Quote #1

Hey,

I bet I again made silly mistake, but weird things is happening. I got maths formula that I tested on:
PHP
Java
calculator

and works great, but on SP it outputs me 0.

PHP Code:
         int pointsNew = (65 65 / (25) * 10000); 
What am I doing wrong?
LambdaLambda is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 03-05-2015 , 21:10   Re: Maths formula, what?
Reply With Quote #2

int/+*-%int returns int.
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 03-05-2015 , 21:23   Re: Maths formula, what?
Reply With Quote #3

Right, shame on me. Cheers!
LambdaLambda is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 03-05-2015 , 22:31   Re: Maths formula, what?
Reply With Quote #4

Going into more detail for any newbies who happen across this thread.

You have to think about each individual operation.

65 / 65 / (1 + 25) * 10000
65 / 65 / 26 * 10000
1 / 26 * 10000

Here's where it gets tricky. Since 1 and 26 are both integers, the answer can only be an integer. The real value of 1/26 is around 0.0384, which cannot be represented by an integer. So we round down to the nearest integer, which is zero.

0 * 10000

= 0
__________________

Last edited by ddhoward; 03-05-2015 at 22:32.
ddhoward is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-05-2015 , 22:41   Re: Maths formula, what?
Reply With Quote #5

Quote:
Originally Posted by ddhoward View Post
Going into more detail for any newbies who happen across this thread.

You have to think about each individual operation.

65 / 65 / (1 + 25) * 10000
65 / 65 / 26 * 10000
1 / 26 * 10000

Here's where it gets tricky. Since 1 and 26 are both integers, the answer can only be an integer. The real value of 1/26 is around 0.0384, which cannot be represented by an integer. So we round down to the nearest integer, which is zero.

0 * 10000

= 0
I'm pretty sure C++(which is the code running SM plugins) truncates floats when doing int dividing by int. eg:
5 /3 = 1 (1.6 repeat truncates to 1)
__________________

Last edited by WildCard65; 03-05-2015 at 22:42.
WildCard65 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-06-2015 , 10:07   Re: Maths formula, what?
Reply With Quote #6

Quote:
Originally Posted by WildCard65 View Post
I'm pretty sure C++(which is the code running SM plugins) truncates floats when doing int dividing by int. eg:
5 /3 = 1 (1.6 repeat truncates to 1)
SourceMod has a Pawn VM built into it, so I wouldn't be so quick to assume that's why this is happening.

No, it's in the Pawn language definition. Just like it is in language definitions for C, C++, C#, and Java just to name a few.

As a general rule1, a strongly typed language is going to return the result of a simple math operation */+- as the type with the largest range used in the operation. For Pawn, this means int / int == int, float / int == float

1That's not always the case, however... for instance, you'll be stung by this in C# if you try adding a float/double and a decimal as there is no implicit conversion from float/double to decimal... you have to explicitly convert the float/double first.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 03-06-2015 , 16:12   Re: Maths formula, what?
Reply With Quote #7

Yeah, that was the problem, as I said on my previous post. This used to be converting by itself on old syntax, so I was a bit confused. But I done some research, I'm on a right track now. Thank you! : )

Last edited by LambdaLambda; 03-06-2015 at 18:33.
LambdaLambda is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 03-06-2015 , 17:42   Re: Maths formula, what?
Reply With Quote #8

I'm not sure if it actually always converts in the old syntax. I've seen weird results where after an update or something my functions that used to mix floats and ints would get messed up and give results like 253,793 or something, in the exact same scenarios as when they used to work.
__________________
Chdata is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:34.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode