# FIXED: Mod and \\ - running into 32 bit Math problems on a 64 bit system 🤦‍♂️

**URL:** <https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255>\
**Category:** Other\
**Created:** [28 October 2020 04:20 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255 "2020-10-28T04:20:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [28 October 2020 04:20 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/1 "2020-10-28T04:20:29Z")

</div>

For a forthcoming article in xDev I had to use the Mod function and came across an interesting problem:

From the Xojo documentation:

> **result = number1 Mod number2**
> 
> The Mod operator divides number1 by number2 and returns the remainder as result. If either number1 or number2 is a floating-point type, it is first coerced to Int32. If either number1 or number2 is an Int64, then both values are promoted to Int64.

Result is an Integer, and should be a 64 bit integer on a 64 bit system. Testing revealed that the coercing to 32bit Integer rule takes precedence over promoting to 64bit Integer.

But why would the double be coerced into a 32bit integer on 64 bit systems? That just screams overflow error!

Basically you are running into a 32 bit limitation on a 64 bit system.

Have Xojo forgotten to update this when they moved to 64 bit?

And now don’t have a compiler guy anymore to fix it?

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [28 October 2020 05:09 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/2 "2020-10-28T05:09:55Z")

</div>

🤷‍♂️

---

<div class="post-metadata">

**Author:** ![s7g2vp2](https://ifnotnil.com/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@s7g2vp2](https://ifnotnil.com/u/s7g2vp2)\
**Post date:** [28 October 2020 06:56 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/3 "2020-10-28T06:56:50Z")

</div>

Min & Max have a similar problem as they expect doubles rather than being overloaded for Int64 / UInt64.

Case logged in Feedback but I would be surprised if it got fixed.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [28 October 2020 07:39 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/4 "2020-10-28T07:39:32Z")

</div>

I bet this is just a documentation issue as the CPUs can easily do mod for 64-bit.

```
Dim n As Int64 = 300000000002

Dim x As Int64 = n Mod 3
Dim y As Int64 = n \ 3
Break

```

Works well here.

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [28 October 2020 07:54 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/5 "2020-10-28T07:54:08Z")

</div>

Sorry, Christian, you are missing the point: try it with a double and you’ll see the problem.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [28 October 2020 08:21 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/6 "2020-10-28T08:21:25Z")

</div>

You are right. Double is converted to int32 and doing the mod and div operation. How bad.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [28 October 2020 08:23 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/7 "2020-10-28T08:23:59Z")

</div>

Feedback case 11536: mod fails for doubles with divisors greater than [( 2 ^ 31 ) - 2]

is already 10 years old 🙄

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [28 October 2020 08:44 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/8 "2020-10-28T08:44:23Z")

</div>

Thanks for digging that one out.

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [28 October 2020 11:06 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/9 "2020-10-28T11:06:44Z")

</div>

> [@MarkusWinter](#):
>
> And now don’t have a compiler guy anymore to fix it?

They do most stuff themselves. e.g. William does a few things. And I bet they have the staff to find the conversion from double to Int32 in the code generating the Intermediate Representation for LLVM for a MOD operation and change it to Int64.

by the way, I don’t use mod often. And even less often with double as input. This is not an issue affecting a lot of people.

---

<div class="post-metadata">

**Author:** ![trishaduke](https://ifnotnil.com/user_avatar/ifnotnil.com/trishaduke/32/41_2.png) [@trishaduke](https://ifnotnil.com/u/trishaduke)\
**Post date:** [28 October 2020 11:47 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/10 "2020-10-28T11:47:33Z")

</div>

> [@MonkeybreadSoftware](#):
>
> by the way, I don’t use mod often. And even less often with double as input. This is not an issue affecting a lot of people.

i do use MOD occasionally but never with float/double… always with whole number

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [28 October 2020 12:36 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/11 "2020-10-28T12:36:14Z")

</div>

> [@MarkusWinter](#):
>
> And now don’t have a compiler guy anymore to fix it?

now WHY would you say that ? 😛

How about since geoff says they dont have a dedicated compiler guy ?  
(from [Something funky about UInt32 comparisons - #163 by Geoff\_Perlman - General - Xojo Programming Forum](https://forum.xojo.com/t/something-funky-about-uint32-comparisons/57447/163))

> [@](#):
>
> It’s not a question of technical skills as the release notes will tell you. We have fixed many compiler bugs in the past several years since we had a dedicated compiler engineer and have made the transition from 32 to 64 bit for the compiler as well. We have the talent we need.

Which flat out says “we dont have a dedicated compiler guy”

---

<div class="post-metadata">

**Author:** ![doj](https://ifnotnil.com/user_avatar/ifnotnil.com/doj/32/530_2.png) [@doj](https://ifnotnil.com/u/doj)\
**Post date:** [28 October 2020 13:47 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/12 "2020-10-28T13:47:39Z")

</div>

so we know the issue, although as others mentioned, I use MOD a lot but never with FP or doubles (old embedded code habit of making EVERYTHING integer).

what is a simple work around, turn everything to int64 first and then convert back?

---

<div class="post-metadata">

**Author:** ![MonkeybreadSoftware](https://ifnotnil.com/user_avatar/ifnotnil.com/monkeybreadsoftware/32/451_2.png) [@MonkeybreadSoftware](https://ifnotnil.com/u/MonkeybreadSoftware)\
**Post date:** [28 October 2020 15:11 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/13 "2020-10-28T15:11:38Z")

</div>

Integers should work.

Just don’t do MOD with double or single data types.

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [28 October 2020 20:35 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/14 "2020-10-28T20:35:13Z")

</div>

> [@MonkeybreadSoftware](#):
>
> by the way, I don’t use mod often. And even less often with double as input. This is not an issue affecting a lot of people.

The basic problem is that under a 32 bit system a Double can represent a MUCH larger range of integers accurately than an Integer can. So a LOT of code used Double instead of Integer to avoid integer overflows:

The maximum value of “whole numbers” that can be accurately represented:

| | 32 bit | 64 bit |
| --- | --- | --- |
| Integer | 2,147,483,647 | 9,223,372,036,854,775,807 |
| Double | 9,007,199,254,740,992 | 9,007,199,254,740,992 |
| unsigned Integer | 4,294,967,295 | 18,446,744,073,709,551,615 |

On 32 bit systems it made some sense to cast to a 32 bit integer even though I would argue it would have been better (and more future proof) to cast to a 64 bit integer (and yes, you CAN use 64 bit integers on 32 bit systems though it will probably slow down the calculation).

However on 64 bit systems it is simply not acceptable to cast a Double to 32 bit Integers.

**Btw the two Boeings that fell out of the air because their engines stalled? That was due to a 32bit Integer overflow error.**

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [28 October 2020 21:42 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/15 "2020-10-28T21:42:39Z")

</div>

probably a long standing issue since the sized integer types were added

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [28 October 2020 21:56 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/16 "2020-10-28T21:56:12Z")

</div>

Alberto brought up a nice example with date intervals where the problem rears it’s ugly head:

> **[Xojo Math badly broken: 32bit problems in a 64bit environment 🤦‍♂️](https://forum.xojo.com/t/xojo-math-badly-broken-32bit-problems-in-a-64bit-environment/58288/13)**
>
> I tried your code. At first I thought I could not repeat the problem but then I realized that I needed to add another column to the Listbox 🤦‍♂️ and I wonder if the reviewers had the same problem - it is better to submit an example project! Now your...

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [28 October 2020 22:01 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/17 "2020-10-28T22:01:08Z")

</div>

![screenshot_211](https://ifnotnil.com/uploads/default/original/2X/b/b640e4cf72d6069544bbe66b9badfb6a9cd104fe.png)  
That simply should not happen on a 64 bit system.

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [30 October 2020 08:47 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/18 "2020-10-30T08:47:02Z")

</div>

> [@Christian\_Schmitz](#):
>
> In feedback case 11536 got implemented by Paul. Great if he can do that and I hope they verified it’s working better now.
> 
> @Paul\_Lefebvre, did you also do change for \ operator?

WOOHOO!

Took 10 years and Christian’s detective work, but good news nonetheless!

👍

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [30 October 2020 17:42 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/19 "2020-10-30T17:42:44Z")

</div>

… and the same bug in Integer Division got fixed too!

👍

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 October 2020 17:54 UTC](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255/20 "2020-10-30T17:54:02Z")

</div>

are they going to fix thoms bug too where the comparison fails ?  
seems pointless to only fix some of them

[Next page](https://ifnotnil.com/t/fixed-mod-and-running-into-32-bit-math-problems-on-a-64-bit-system/1255.md?page=2)
