Skip to content

Fix loss of precision when converting float to int#39

Merged
nicktelford merged 1 commit intomasterfrom
fix-precision-loss
Feb 12, 2025
Merged

Fix loss of precision when converting float to int#39
nicktelford merged 1 commit intomasterfrom
fix-precision-loss

Conversation

@nicktelford
Copy link
Contributor

When calling .int() on a floating-point number, we were incorrectly
marshalling to a 32-bit integer (aka. Int), instead of a 64-bit integer (aka.
Long), which could result in the result being bounded at 2147483647
(the maximum signed 32-bit integer) unnecessarily.

Since all "integers" in IDML are 64-bit, there's no reason to marshall a
floating point number to a 32-bit number.

Example:

json> { "foo": 1739180023809489.0 }
~> bar = foo.int()
..
{
  "bar" : 2147483647
}

Changing to toLong yields this result:

json> { "foo": 1739180023809489.0 }
~> bar = foo.int()
..
{
  "bar" : 1739180023809489
}

This should not cause any regressions, as it is unlikely that any users
are depending on this "bounding" behaviour.

When calling `.int()` on a floating-point number, we were incorrectly
marshalling to a 32-bit integer (aka. `Int`), instead of a 64-bit integer (aka.
`Long`), which could result in the result being bounded at `2147483647`
(the maximum signed 32-bit integer) unnecessarily.

Since all "integers" in IDML are 64-bit, there's no reason to marshall a
floating point number to a 32-bit number.

Example:
```
json> { "foo": 1739180023809489.0 }
~> bar = foo.int()
..
{
  "bar" : 2147483647
}
```

Changing to `toLong` yields this result:
```
json> { "foo": 1739180023809489.0 }
~> bar = foo.int()
..
{
  "bar" : 1739180023809489
}
```

This should not cause any regressions, as it is unlikely that any users
are depending on this "bounding" behaviour.
@nicktelford nicktelford merged commit d1732f2 into master Feb 12, 2025
@nicktelford nicktelford deleted the fix-precision-loss branch February 12, 2025 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant