Raised This Month: $12 Target: $400
 3% 

Solved Nonsense Divide


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ConorCC
Member
Join Date: Feb 2014
Old 06-21-2021 , 06:49   Nonsense Divide
Reply With Quote #1

Hello there!

Can some explain why

static item_count; item_count = 0;
client_print(id, print_chat, "Item Count: %i | Page: %i", item_count, (item_count - 1) / 7);

prints "Item Count: 0 | Page: -1"

instead of

prints "Item Count: 0 | Page: 0"

So why -1 divided by 7 is -1 instead of 0? Shouldn't be it zero?

Please don't think me stupid.

Last edited by ConorCC; 06-26-2021 at 17:07. Reason: Solved
ConorCC is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 06-21-2021 , 09:42   Re: Nonsense Divide
Reply With Quote #2

The result of -1 divided by 7 is -0.14285714285. In your example, the value is probably being rounded.
If what you are trying to do is calculate the total number of pages, this is the correct formula:
Code:
floatround(float(total item count) / float(limit per page), floatround_ceil)
__________________








CrazY. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-21-2021 , 22:25   Re: Nonsense Divide
Reply With Quote #3

Quote:
Originally Posted by ConorCC View Post
Hello there!

Can some explain why

static item_count; item_count = 0;
client_print(id, print_chat, "Item Count: %i | Page: %i", item_count, (item_count - 1) / 7);

prints "Item Count: 0 | Page: -1"

instead of

prints "Item Count: 0 | Page: 0"

So why -1 divided by 7 is -1 instead of 0? Shouldn't be it zero?

Please don't think me stupid.
Instinctively, I would expect it to be zero. I created a quick C++ application on Linux and it's properly showing -1/7 = 0.

More importantly, I don't think item count should ever be zero. If it's zero, then there is no concept of pages so it doesn't make sense to print that value.
__________________

Last edited by fysiks; 06-21-2021 at 23:35.
fysiks is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-22-2021 , 05:53   Re: Nonsense Divide
Reply With Quote #4

From "Pawn The Language":
Quote:
e1 / e2
Results in the division of e1 by e2. The result is truncated to the nearest integral value that is less than or equal to the quotient. Both negative and positive values are rounded down, i.e. towards −∞.
It just depends on how language designers decided. There are other mainstream languages that round towards -∞ too. There's no right or wrong choice here, it's just important to be consistent.
__________________

Last edited by klippy; 06-22-2021 at 05:54.
klippy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-23-2021 , 01:16   Re: Nonsense Divide
Reply With Quote #5

Quote:
Originally Posted by klippy View Post
From "Pawn The Language":

It just depends on how language designers decided. There are other mainstream languages that round towards -∞ too. There's no right or wrong choice here, it's just important to be consistent.
I guess I would have figured that integer division was so straight forward a concept that it couldn't be interpreted in so many ways. But clearly, that's not the case, go figure.
__________________
fysiks 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 10:00.


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