Raised This Month: $ Target: $400
 0% 

Solved Mathematical Problem


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-17-2018 , 14:51   Re: Mathematical Problem
Reply With Quote #6

The problem is that you are dividing an integer with an integer just like fysiks said and after that you multiply by 100.
Here's a simple example to show you why it doesn't work:
Code:
    server_print("1: %.2f", 5/3*100.0); // 1: 100.00 (5/3 = 1, 1*100 = 100)     server_print("2: %.2f", 5*100.0/3); // 2: 166.66 (5*100 = 500, 500/3 = 166.66)

As you can see, as long as the multiplication is done before division the result is what you expect.

Here are the options you have.
Code:
    // Saved as integer, move the multiplication.     new intVar1 = floatround(1 * 100.0 / 3)     server_print("1: %.1f / %d", float(intVar1), intVar1); // 1: 33.0 / 33     // Saved as integer, turn it into float before dividing.     new intVar2 = floatround(float(1) / 3 * 100.0)     server_print("2: %.1f / %d", float(intVar2), intVar2); // 2: 33.0 / 33     // Saved as float, move the multiplication.     new Float:flVar3 = 1 * 100.0 / 3     server_print("3: %.1f / %d", flVar3, floatround(flVar3)); // 3: 33.3 / 33     // Saved as float, turn it into float before dividing.     new Float:flVar4 = float(1) / 3 * 100.0     server_print("4: %.1f / %d", flVar4, floatround(flVar4)); // 4: 33.3 / 33
I suggest the first option for simplicity.
__________________

Last edited by Black Rose; 04-17-2018 at 15:09.
Black Rose is offline
 



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 04:33.


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