Raised This Month: $32 Target: $400
 8% 

Solved Problem with division


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 06-18-2017 , 21:22   Problem with division
Reply With Quote #1

I feel like this error is something simple, but I cannot figure it out.

PHP Code:
PrintToConsole(client"TEST2: %f"10); 
Output:

PHP Code:
TEST20.000000 

Last edited by RumbleFrog; 06-18-2017 at 23:29.
RumbleFrog is offline
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 06-18-2017 , 21:25   Re: Problem with division
Reply With Quote #2

The issue is that it outputs 0.000000 instead of 0.8
RumbleFrog is offline
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 06-18-2017 , 21:39   Re: Problem with division
Reply With Quote #3

try
PHP Code:
PrintToConsole(client"TEST2: %f"8.0 10.0); 
8 = integer
10 = interger
8.0 = float
10.0 = float
__________________
coding & free software
shanapu is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 06-18-2017 , 21:41   Re: Problem with division
Reply With Quote #4

edit: shanapu sniped me.

Quote:
Originally Posted by RumbleFrog View Post
I feel like this error is something simple, but I cannot figure it out.

PHP Code:
PrintToConsole(client"TEST2: %f"10); 
Output:

PHP Code:
TEST20.000000 
You're dividing integers, so you're going to get back an integer (whose value will be 0).

PHP Code:
float someNum 1/2
Will store a value of 0 inside of someNum.

PHP Code:
float someNum 1/2.0
Will store a value of .5 into someNum.

Either the numerator or the denominator has to be a float in order to get the results you are expecting, but if you must use integer values you can convert from int to float like this.

PHP Code:
float someNum 1/float(2); 

Last edited by headline; 06-18-2017 at 21:43.
headline is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 06-18-2017 , 22:00   Re: Problem with division
Reply With Quote #5

I think when you do integer division and result is (or should be) a float, the result gets truncated instead of rounded. So 8 / 10 = 0.8. But cus its integers the 8 gets cut off and you're left with 0.
hmmmmm is offline
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 06-18-2017 , 23:29   Re: Problem with division
Reply With Quote #6

Thanks, everyone! This is now solved.
RumbleFrog is offline
Reply


Thread Tools
Display Modes

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:27.


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