#Does zig or lib have a function to round off float values up to a decimal?

1 messages · Page 1 of 1 (latest)

undone bison
#

@round will round to the nearest integer

#

you just need to multiply, round, the divide to round to some number of digits

silk zinc
#

i dont think that's a correct implementation

#
#

also what about rounding to a negative number of digits?

#

isnt roundTo(123.456, -1) = 120 reasonable?

ashen thorn
#

Never needed it

silk zinc
#

more important thing is that your implementation is incorrect

ashen thorn
#

Not anymore

silk zinc
#

now it doesn't work for comptime_float

#

quite ugly but seems to work: https://godbolt.org/z/WosKKrqcY

silk zinc
#

and should be fairly efficient

#

you could probably make it a lot faster but should be within a few orders of magnitude of optimal

#

where is T defined?

#

oh my implementation doesnt work if value isnt comptime but exponent is

#

now it works with comptime ints too: https://godbolt.org/z/YYrE4rKfG

#

wait what am i doing it should just only work for powers of 10 lmao

#

yeah but what about negative values?

#

is a max of 127 enough?

#

right thats way beyond the precision

#

of anything >1

#

either way i think youve got enough ideas to make something that suits your usecase

#

glhf

#

np

silk zinc
#

is this rounding a bottleneck?