Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Unexpected result with ceil()

Author
Message
Kevin Cross
21
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 25th Aug 2017 17:21 Edited at: 25th Aug 2017 17:22
To me this seems like a bug as it doesn't give the result I'd expect when passing integers into ceil. Here's some examples

I'd expect 2 with this example but I get 1 in AGK:

ceil(7 / 4)

I only get 2 if I do this:

ceil(7.0 / 4.0)

I noticed this problem because I'm dividing two integer variables: noOfCards and noOfCols. noOfCards would be a different number each time and noOfCols would always be 4.

ceil(noOfCards / noOfCols)

It works if I do this:

ceil(noOfCards * 1.0 / noOfCols * 1.0)

To me it's a bug because if I type ceil(7 / 4) in the Mac Spotlight search box I get 2, not 1.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 25th Aug 2017 17:59
integer division
print(8 / 4)
is 2
print(7 / 4)
is 1

me would expect integer = integer / integer than cast into float because its input at ceil.

AGK (Steam) V2017.08.16 : Windows 10 Pro 64 Bit : AMD (17.7.2) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Kevin Cross
21
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 26th Aug 2017 12:35
Not sure what you're saying but this is the result in other languages:

JavaScript: Math.ceil(7 / 4) = 2
PHP: ceil(7 / 4) = 2
Python: math.ceil(7 / 4) = 2

That's what I'd expect in AGK
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 26th Aug 2017 13:21 Edited at: 26th Aug 2017 13:23
CPU's have both Integer and Floating pointer division (and other numeric operations). There's various rules used across most languages, such as casting everything as float, similarly everything can be assumed to be an integer operation. In most BASIC's numeric types (integers & floats) are treated the same, so you don't need to explicitly cast all the time, but there are exceptions. Primarily If you divide a pair of integers, the compiler will choose an integer division and produce and integer result but if either is float or both are floats it'll choose to use floating point division on the FPU. So if you have a integer values and want to force a floating point divide/ multi[y/ addition / subtraction then one of the terms needs to be cast as float.

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 26th Aug 2017 14:27 Edited at: 26th Aug 2017 14:29
i meant it is not ceil its agk itself how to handle values.
seems agk is different to much other languages.

i remember a basic where you can use / or \
i believe \ was explicit for integer division there.
AGK (Steam) V2017.08.16 : Windows 10 Pro 64 Bit : AMD (17.7.2) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
MikeHart
AGK Bronze Backer
21
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 27th Aug 2017 06:26
AGK is not much different. I can name quite a few languages where when you do an integer division. You get an integer result.
Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200. Using AGK2 Tier 1.
Kevin Cross
21
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 27th Aug 2017 08:48 Edited at: 27th Aug 2017 08:50
EDIT: Removed

Login to post a reply

Server time is: 2024-11-24 09:58:45
Your offset time is: 2024-11-24 09:58:45