Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Problem with percentage

 
  • 0 Vote(s) - 0 Average
Pawn Problem with percentage
GospodinX
Offline

Burgershot Member
Posts: 10
Threads: 5
Joined: Dec 2020
Reputation: 0
Location: Bosna i Hercegovina
#1
2020-12-22, 10:32 AM
Hi guys

I use percentage for many things in my SA-MP server.(discounts,tax etc),but I have some problems with it.

For "small" numbers it work pretty nice.

Code:

Code:
new result = PERCENT*PRICE/100;

printf("Result: %d",result);


First test:
PERCENT: 75
PRICE: 140 000 000
Result: 19100654

Result is ok

Second test:
PERCENT: 75
PRICE: 340 000 000
Result: -17698038

Result is wrong


Does anyone have idea how I should fix it?
Pinch
Offline

Burgershot Member
Posts: 391
Threads: 19
Joined: Apr 2019
Reputation: 22
Location: Belgrade, Serbia
#2
2020-12-22, 11:36 AM
Because -2³¹-1 is the max value a single variable can store and as soon as you can try to go beyond that value, it overflows and starts going backwards...

Try price * 0.percentage

Ex: 340 000 000 * 0.75
Using Pawn.CMD?
If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
GospodinX
Offline

Burgershot Member
Posts: 10
Threads: 5
Joined: Dec 2020
Reputation: 0
Location: Bosna i Hercegovina
#3
2020-12-22, 10:45 PM
(2020-12-22, 11:36 AM)Pinch Wrote: Because -2³¹-1 is the max value a single variable can store and as soon as you can try to go beyond that value, it overflows and starts going backwards...

Try price * 0.percentage

Ex: 340 000 000 * 0.75

Hm ?

[Image: Fcsjxv6.png]
Pinch
Offline

Burgershot Member
Posts: 391
Threads: 19
Joined: Apr 2019
Reputation: 22
Location: Belgrade, Serbia
#4
2020-12-23, 10:54 AM
My bad, price - (price * 0.75)
Using Pawn.CMD?
If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode