#Safely power a F64?

19 messages · Page 1 of 1 (latest)

rigid wigeon
#

I know how to do F64::powi(2) but how would I check if that gets overloaded and be able to handle that?

gloomy cave
#

I guess you can call is_infinite() to check

rigid wigeon
#

Alright

worn atlas
#

Rust doesn’t have method overloading

#

if you have a type, and you know it’s an f64, .powi will always call the same function

#

no matter the context

molten tangle
#

Looks like f32/f64 don't have any checked_ functions, which is a little weird

worn atlas
#

oh, you meant over_flow_ 😄

finite pulsar
#

whaaat
this seems like a big oversight

#

or maybe we're missing something which i really hope is what's happening

hazy scarab
#

I’m not really sure floats have overflow in the same way integers do

#

Floats just converge to infinity iirc

#

So the only real "overflow check" you’d do would be like rust fn checked_powi(f: f64, pow: u32) -> Option<f64> { let power = f.powi(pow); if power.is_infinite() { None } else { Some(power) } }

molten tangle
#

Yeah it would just be that, which while not as crucial as the integer ones, because those can't be emulated, I think it would be good to have a consistent language for this kind of stuff

hazy scarab
#

Sure, I think it’s just an effect of floats not really having a concept of checking

#

And the applications using them not actually needing it

echo heart
#

processors actually do have flags that can turn e.g. NaN creation (not sure what else; couldn't find docs quickly) into a trap/interrupt; the problem is that very few languages let you actually enable that in a way that isn't UB-or-implementation-defined, because the CPU having state that changes what instructions do is inconvenient to deal with

molten tangle
#

And people not usually caring about numbers big enough to make an f64 go infinite