IRC channel logs

2023-08-30.log

back to list of logs

<old>what is the way to convert a integer that has a zero fractional part to a true integer that can be used with vector-ref for example?
<old>e.g.: (vector-ref histogram (/ 0.5 0.1)) <=> (vector-ref histogram 5.0)
<old>The 5.0 is problematic here. How can I make it to only 5
<haugh>old, inexact->exact
<old>(inexact->exact) would work for this case, but in other case it would give a fraction
<old>(/ 0.6 0.1)
<old>-> 6755399441055743/1125899906842624
<old>is there a way to just extract the non-fractional part of a number?
<haugh>Ah
<haugh>Floor?
<old>floor does not work also
<old>(floor 6.0) => 6.0
<haugh>(compose inexact->exact floor)
<old>floor seems to make 6.0 to 5
<old>but ceiling would do for that
<old>thanks!
<rlb>old: depending on what you want perhaps (inexact->exact (round x)) would suffice.
<old>rlb: I'm quite confuse by why it works
<old>(inexact->exact (round (/ 0.6 0.1))) => 6
<old>(round (/ 0.6 0.1)) => 6.0
<old>(inexact->exact (/ 0.6 0.1)) => 6755399441055743/1125899906842624
<old>Is this a REPL thing
<mirai>I'd do (inexact->exact (round-quotient 0.6 0.1))
<mirai>old: that 6.0 you got after round (or if you were to type in) would be represented as 0x40C00000 (binary32)
<mirai>which does round to the integer 6
<mirai>that division also gives the same result in binary32
<mirai>however… things aren't so with binary64
<mirai>6.0 => 0x4018000000000000 ; (/ 0.6 0.1) = 5.999999999999999 => 0x4017FFFFFFFFFFFF
<mirai>I don't know what precision Guile does its stuff in
<mirai>but your results seem to be related to precision
<mirai>btw inexact->exact doesn't mean that you get an integer
<mirai>you get a rational
<old>mirai: I see. weird thing can happen hm
<old>something something floating point yet again
<old>And right. Integer are rational so that make sens to get a huge fraction if the result is not round properly
<haugh>old, not sure about your larger application but you can start with rationals to avoid float quirks
<haugh>(/ 6/10 1/10)
<haugh>I avoid floats at all costs
<old>hmm that's not a bad idea at all
<old>I only have to change a single constant in my code : 0.1 => 1/10
<old>and it fixes the issue
<haugh>sweet
<old>thanks!
<haugh>Can I return the module from which I obtained a given binding?