Is there a simpler way to do math between ints and floats?

I’m doing a bit of math between ints and floats, and converting ints to floats seems to take up most of my screen. For instance, these three lines:

   // The furthest the user can hit
   let maxDistanceIndex = fairway
      -> A.findIndex(it => Int.toFloat(it.yards) <= maxDistance)
   
   // The LIVE value from the selector, from 0-1
   let (yPercent, setYPercent) = 
      React.useState(() => Int.toFloat(maxDistanceIndex) /. Int.toFloat(itemCount))
   
   // the LIVE selection
   let activeSelection = (Int.toFloat(itemCount) *. yPercent)
      -> Math.round

All this casting is making some simple maths a bit more difficult to read, and I’m wondering if this is just how it’s meant to be done. Or is there an easier/recommended way?

How about aliasing a helper?

let float = Int.toFloat

Then,

// The furthest the user can hit
   let maxDistanceIndex = fairway
      -> A.findIndex(it => float(it.yards) <= maxDistance)
   
   // The LIVE value from the selector, from 0-1
   let (yPercent, setYPercent) = 
      React.useState(() => float(maxDistanceIndex) /. float(itemCount))
   
   // the LIVE selection
   let activeSelection = (float(itemCount) *. yPercent)
      -> Math.round

The helper name could be even shorter, of course, e.g. f.

1 Like

That helper float already exists globally!

4 Likes

Oh wow, TIL! It’s defined in OCaml’s Stdlib.

1 Like

An interesting shadowy area in Rescript code… I was getting errors about JS_OO.Callback.arity1 the other day…same kind of thing

And it looks zero-cost.

2 Likes

This link didn’t work :slight_smile:

My bad, I copied non-shareable link. Updated my comment.