Skip to content

Commit

Permalink
first relative test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Aug 26, 2024
1 parent dfebe5f commit d085040
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
34 changes: 30 additions & 4 deletions FrontEnd/Lib/fmt_real.cw
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ https://www.ryanjuckett.com/printing-floating-point-numbers/"""
(return t)))))


(fun slice_incp [(param s (slice! u8)) (param inc uint)] (slice! u8) :
(let n uint (min inc (len s)))
(return (slice_val (pinc (front! s) n) (- (len s) n))))


@doc """for a given float val we want to find a decomposition
val = x * 10^t so that 2^53 / 10 < x <= 2^53
because this way we can compute the base ten digits easily.
Expand All @@ -54,6 +59,27 @@ radix character shall appear. The low-order digit shall be rounded in an impleme
manner. The E conversion specifier shall produce a number with 'E' instead of 'e' introducing
the exponent. The exponent shall always contain at least two digits. If the value is zero,
the exponent shall be zero."""
@pub (fun FmtExponentE [(param exp s32) (param out (slice! u8))] uint :
(if (< (len out) 3) :
(return 0)
:)
(let! i auto 1_uint)
(if (< exp 0) :
(= (at out 0) '-')
(if (>= exp -9) :
(= (at out 1) '0')
(+= i 1)
:)
(return (+ i (fmt_int::FmtDec@ [(~ exp) (slice_incp [out i])])))
:
(= (at out 0) '+')
(if (<= exp 9) :
(= (at out 1) '0')
(+= i 1)
:)
(return (+ i (fmt_int::FmtDec@ [exp (slice_incp [out i])])))))


@pub (fun FmtE@ [
(param val r64)
(param precision u32)
Expand All @@ -67,7 +93,7 @@ the exponent shall be zero."""
(return 0)
:)
(let! t s32 (find_t [val]))
(fmt::print# "@@@ " t "\n")
@doc """fmt::print#("@@@ ", t, "\n")"""
(let x auto (div_by_power_of_10 [val t]))
(let! mantissa auto (+ (num_real::r64_raw_mantissa [x]) (<< 1 52)))
(let exponent auto (- (num_real::r64_raw_exponent [x]) num_real::r64_exponent_bias))
Expand All @@ -80,7 +106,7 @@ the exponent shall be zero."""
:)
(let! buffer (array 32 u8) undef)
(let num_digits uint (fmt_int::FmtDec@ [mantissa (as buffer (slice! u8))]))
@doc "round if we drop digits"
@doc "decimal rounding if we drop digits"
(if (> num_digits (as (+ precision 1) uint)) :
(let! pos auto (+ precision 2))
(let! carry bool (>= (at buffer pos) '5'))
Expand Down Expand Up @@ -120,11 +146,11 @@ the exponent shall be zero."""
(+= i 1))
(= (at out i) 'e')
(+= i 1)
(let num_exp_digits auto (fmt_int::FmtDec@ [t (as buffer (slice! u8))]))
(let num_exp_digits auto (FmtExponentE [t (as buffer (slice! u8))]))
(for j 0 num_exp_digits 1 :
(= (at out i) (at buffer j))
(+= i 1))
(fmt::print# "@@@ " t " " exponent " " buffer " out:" out "\n")
@doc """fmt::print#("@@@ ", t, " ", exponent, " ", buffer, " out:", out, "\n")"""
(return i))
)

36 changes: 26 additions & 10 deletions FrontEnd/Lib/fmt_real_test.cw
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@

(import num_real)

(fun slice_incp [(param s (slice! u8)) (param inc uint)] (slice! u8) :
(let n uint (min inc (len s)))
(return (slice_val (pinc (front! s) n) (- (len s) n))))


(fun test_nan [] void :
@doc "sanity checks for NANs"
(test::AssertNe# +inf_r64 -inf_r64))


(fun test_normal [(param multiplier s32) (param precision uint)] void :
(fun test_normal [(param multiplier u32)
(param exp10 s32)
(param precision uint)] void :
(let! expected (array 64 u8) undef)
(let! expected2 (array 64 u8) undef)
(let! buf (array 1024 u8) undef)
@doc "sanity checks for NANs"
(for i 0 -294_sint -1 :
(let! val r64 (as multiplier r64))
(/= val (at num_real::powers_of_ten (~ i)))
(do (fmt_real::FmtE@ [val 8 true buf]))

(let! actual (array 1024 u8) undef)
(let! val r64 (as multiplier r64))
(/= val (at num_real::powers_of_ten (~ exp10)))
(let len_a uint (fmt_real::FmtE@ [val 8 true actual]))
(= (at expected 0) '+')
(= (at expected 1) (+ '0' (as multiplier u8)))
(= (at expected 2) '.')
(for j 0 precision 1 :
(= (at expected (+ j 3)) '0')
)
(= (at expected (+ precision 3)) 'e')
(let! len_e uint (fmt_real::FmtExponentE [exp10
(slice_incp [expected (+ precision 4)])]))
(+= len_e precision)
(+= len_e 4)
(test::AssertSliceEq# (slice_val (front expected) len_e)
(slice_val (front actual) len_a))
)


Expand All @@ -33,7 +47,9 @@
(fmt::print# (bitwise_as 0x0p0_r64 u64) "\n")
"""
(do (test_nan []))
(do (test_normal [1 8]))
(for i 0 -294_s32 -1 :
(do (test_normal [1 i 8]))
)
(test::Success#)
(return 0))
)

0 comments on commit d085040

Please sign in to comment.