From 9e232c7e65d54312adba716ea7968f7e4a4e878a Mon Sep 17 00:00:00 2001 From: TREE_3 Date: Thu, 20 Feb 2025 11:17:58 +0000 Subject: [PATCH] !216 rich-integer%sqrt in (liii lang) --- GoldfishLang.tmu | 54 +++++++++++++++++++++++++++++++ goldfish/liii/lang.scm | 7 ++++ tests/goldfish/liii/lang-test.scm | 14 ++++++++ 3 files changed, 75 insertions(+) diff --git a/GoldfishLang.tmu b/GoldfishLang.tmu index 6404652e..35704df8 100644 --- a/GoldfishLang.tmu +++ b/GoldfishLang.tmu @@ -1347,6 +1347,60 @@ \; + \; + + + + + + <\goldfish-chunk|goldfish/liii/lang.scm|true|true> + ;;return exact integer + + (define (%sqrt) + + \ \ (if (\ data 0) + + \ \ \ \ \ \ (value-error + + \ \ \ \ \ \ \ \ (format #f "sqrt of negative integer is undefined! \ \ \ \ \ \ \ \ ** Got ~a **" data)) + + \ \ \ \ \ \ (inexact-\exact (floor (sqrt data))))) + + \; + + + \; + + <\goldfish-chunk|tests/goldfish/liii/lang-test.scm|true|true> + (check ($ 0 :sqrt) =\ 0) \ \ \ \ \ \ + + (check ($ 1 :sqrt) =\ 1) \ \ \ \ \ \ + + (check ($ 2 :sqrt) =\ 1) \ \ \ \ \ \ + + (check ($ 9 :sqrt) =\ 3) \ \ \ \ \ \ + + (check ($ 8 :sqrt) =\ 2) + + (check ($ 10 :sqrt) =\ 3) + + (check ($ 144 :sqrt) =\ 12) \ \ \ \ \ \ + + (check ($ 289 :sqrt) =\ 17) \ \ \ \ \ \ + + (check ($ 290 :sqrt) =\ 17) \ \ \ \ \ \ + + (check ($ 10201 :sqrt) =\ 101) \ \ \ \ \ \ + + (check ($ 10403 :sqrt) =\ 101)\ + + (check ($ (rich-integer :max-value) :sqrt) =\ 3037000499) + + (check-catch 'value-error ($ -1 :sqrt)) + + \; + + <\goldfish-chunk|goldfish/liii/lang.scm|true|true> diff --git a/goldfish/liii/lang.scm b/goldfish/liii/lang.scm index 34080bcb..d0328a96 100644 --- a/goldfish/liii/lang.scm +++ b/goldfish/liii/lang.scm @@ -291,6 +291,13 @@ (define (@min-value) -9223372036854775808) +;;return exact integer +(define (%sqrt) + (if (< data 0) + (value-error + (format #f "sqrt of negative integer is undefined! ** Got ~a **" data)) + (inexact->exact (floor (sqrt data))))) + ) (define-case-class rich-char ((code-point integer?)) diff --git a/tests/goldfish/liii/lang-test.scm b/tests/goldfish/liii/lang-test.scm index f0090701..b5dcb6a2 100644 --- a/tests/goldfish/liii/lang-test.scm +++ b/tests/goldfish/liii/lang-test.scm @@ -203,6 +203,20 @@ (check (- (rich-integer :min-value) 1) => (rich-integer :max-value)) +(check ($ 0 :sqrt) => 0) +(check ($ 1 :sqrt) => 1) +(check ($ 2 :sqrt) => 1) +(check ($ 9 :sqrt) => 3) +(check ($ 8 :sqrt) => 2) +(check ($ 10 :sqrt) => 3) +(check ($ 144 :sqrt) => 12) +(check ($ 289 :sqrt) => 17) +(check ($ 290 :sqrt) => 17) +(check ($ 10201 :sqrt) => 101) +(check ($ 10403 :sqrt) => 101) +(check ($ (rich-integer :max-value) :sqrt) => 3037000499) +(check-catch 'value-error ($ -1 :sqrt)) + (check-true ((rich-char #x30) :equals (rich-char #x30))) (check-false ((rich-char #x31) :equals (rich-char #x30)))