From 1cfab8d21e31bab3b903d4d7e1b20df675845384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 25 Oct 2020 12:50:40 +0100 Subject: [PATCH] fix rng not being inclusive --- README.md | 76 ++++++++++++++++++++++-------------------- crsn_arith/src/exec.rs | 2 +- examples/flipcoin.csn | 6 ++++ examples/frandom.csn | 8 +++++ 4 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 examples/flipcoin.csn create mode 100644 examples/frandom.csn diff --git a/README.md b/README.md index d53e878..a94c1d1 100644 --- a/README.md +++ b/README.md @@ -557,92 +557,96 @@ Here's an abridged summary of the floating point submodule: (most of these support the shorthand version too - `RW` in place of `Wr Rd`) ``` -; IntToFloat +; Convert int to float (itf Wr Rd) -; FloatToInt (round) +; Convert float to int (round) (fti Wr Rd) -; FloatToInt (ceil) +; Convert float to int (ceil) (ftic Wr Rd) -; FloatToInt (floor) +; Convert float to int (floor) (ftif Wr Rd) -; FloatTest - nan->invalid, infinities->overflow, positive, negative, zero +; Test properties of a float +; Set flags: +; NaN -> invalid +; Infinities -> overflow +; Positive, negative, zero (ftst Rd) -; FloatCompare +; Float compare (fcmp Rd Rd) -; FloatRangeTest +; Float range test (fcmpr Rd Rd'min Rd'max) -; FloatRng +; FloatRng. Unlike rng, frng is exclusive in the higher bound (frng Wr Rd'min Rd'max) ; --- Basic float arith --- -; FloatAdd +; Float add (fadd Wr Rd Rd) -; FloatSub +; Float subtract (fsub Wr Rd Rd) -; FloatMul +; Float multiply (fmul Wr Rd Rd) -; FloatPow +; Float power (fpow Wr Rd Rd'pow) -; FloatRoot +; Float root (froot Wr Rd Rd'root) -; FloatHyp +; Float hyp - sqrt(a*a + b*b) (fhyp Wr Rd Rd) -; FloatDiv +; Float divide (fdiv Wr Wr'rem Rd'a Rd'div) -; FloatMod +; Float modulo (fmod Wr Rd'a Rd'div) -; FloatAbs +; Float abs value (fabs Wr Rd) -; FloatSgn +; Float signum (returns -1 or 1) (fsgn Wr Rd) ; --- Basic trig --- -; FloatSin +; Float sine (fsin Wr Rd) -; FloatAsin +; Float arcsine (fasin Wr Rd) -; FloatCos +; Float cosine (fcos Wr Rd) -; FloatAcos +; Float arccosine (facos Wr Rd) -; FloatTan +; Float tangent (ftan Wr Rd) -; FloatAtan +; Float arctangent (fatan Wr Rd) -; FloatAtan2 +; Float 2-argumenmt arctangent (fatan2 Wr Rd'y Rd'x) -; FloatCot +; Float cotangent (fcot Wr Rd) -; FloatAcot +; Float arccotangent (facot Wr Rd) ; --- Hyperbolic trig --- -; FloatHypSin +; Float hyperbolic sine (fsinh Wr Rd) -; FloatHypAsin +; Float hyperbolic arcsine (fasinh Wr Rd) -; FloatHypCos +; Float hyperbolic cosine (fcosh Wr Rd) -; FloatHypAcos +; Float hyperbolic arccosine (facosh Wr Rd) -; FloatHypTan +; Float hyperbolic tangent (ftanh Wr Rd) -; FloatHypAtan +; Float hyperbolic arctangent (fatanh Wr Rd) -; FloatHypCot +; Float hyperbolic cotangent (fcoth Wr Rd) -; FloatHypAcot +; Float hyperbolic arccotangent (facoth Wr Rd) ``` Wow, thats a lot. I didn't test many of these yet. There may be bugs. -There are also some pre-defined constants: `PI`, `PI_2`, `TAU`, `E` +There are also some pre-defined constants: `PI`, `PI_2` (½×`PI`), `TAU` (2×`PI`), `E` ## Buffers Module diff --git a/crsn_arith/src/exec.rs b/crsn_arith/src/exec.rs index 1392656..5625d5a 100644 --- a/crsn_arith/src/exec.rs +++ b/crsn_arith/src/exec.rs @@ -84,7 +84,7 @@ impl OpTrait for ArithOp { offset + if max == u64::MAX { rand::thread_rng().gen() } else { - rand::thread_rng().gen_range(0, max) + rand::thread_rng().gen_range(0, max + 1) } }; state.write(dst, val)?; diff --git a/examples/flipcoin.csn b/examples/flipcoin.csn new file mode 100644 index 0000000..a42cc3c --- /dev/null +++ b/examples/flipcoin.csn @@ -0,0 +1,6 @@ +( + (ld r0 5) + (:next) + (rng r1 0 1 (z? (lds @cout "(1€)\n")) (else? (lds @cout "(::)\n"))) + (dec r0 (nz? (j :next))) +) diff --git a/examples/frandom.csn b/examples/frandom.csn new file mode 100644 index 0000000..b0e96bd --- /dev/null +++ b/examples/frandom.csn @@ -0,0 +1,8 @@ +( + (:loop) + (frng r0 0.0 1.0) + (fcmp r0 1.0 + (lt? (j :loop)) + (eq? (lds @cout "OK")) + (gt? (lds @cout "OVER!"))) +)