Zulip Chat Archive
Stream: Is there code for X?
Topic: Float to Int
Marcus Rossel (Aug 30 2024 at 13:24):
Is there any way to convert a Float
to an adjacent Int
? Rounding up, down, anything.
Martin Dvořák (Aug 30 2024 at 13:59):
@loogle (Float -> Int)
loogle (Aug 30 2024 at 13:59):
:shrug: nothing found
Edward van de Meent (Aug 30 2024 at 15:14):
you may be able to go via one of variousUInt
types?
Edward van de Meent (Aug 30 2024 at 15:19):
alternatively, use Float.toRat0?
Adam Topaz (Aug 30 2024 at 15:22):
Adam Topaz (Aug 30 2024 at 15:22):
Ha but that gives a float back
Edward van de Meent (Aug 30 2024 at 15:22):
surprising, right?
Julian Berman (Aug 30 2024 at 16:09):
What would it do to NaN
if it didn't return a float
Julian Berman (Aug 30 2024 at 16:10):
(I guess in the original case you don't care, but yeah it seems like any "real" defined coercion needs to decide on what to do with infs an nans?)
Edward van de Meent (Aug 30 2024 at 16:11):
the toRat0
function i referenced returns 0
, but there is also a version with codomain Option Rat
, which returns Option.none
in those cases.
Mario Carneiro (Aug 30 2024 at 20:14):
Using conversion to Rat
is the best option if you want exact values. Unfortunately there is no Rat.round
function yet...
Eric Wieser (Aug 30 2024 at 20:17):
Don't we have docs#round ?
Mario Carneiro (Aug 30 2024 at 20:20):
ah, it's not in batteries yet
Mario Carneiro (Aug 30 2024 at 20:20):
it's also not using ties-to-even
Eric Wieser (Aug 30 2024 at 20:23):
Nor is it documented as not being that
Edward van de Meent (Aug 30 2024 at 20:24):
as a last resort, you can try using Float.toRatParts, but that might be a bit more hands-on
Last updated: May 02 2025 at 03:31 UTC