Skip to content

Commit

Permalink
!216 rich-integer%sqrt in (liii lang)
Browse files Browse the repository at this point in the history
  • Loading branch information
TREE37 authored and da-liii committed Feb 20, 2025
1 parent 7be7150 commit 9e232c7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
54 changes: 54 additions & 0 deletions GoldfishLang.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,60 @@
\;
</goldfish-chunk>

\;

<subsection|其余函数>

<paragraph|rich-integer%sqrt>

<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
;;return exact integer

(define (%sqrt)

\ \ (if (\<less\> data 0)

\ \ \ \ \ \ (value-error

\ \ \ \ \ \ \ \ (format #f "sqrt of negative integer is undefined! \ \ \ \ \ \ \ \ ** Got ~a **" data))

\ \ \ \ \ \ (inexact-\<gtr\>exact (floor (sqrt data)))))

\;
</goldfish-chunk>

\;

<\goldfish-chunk|tests/goldfish/liii/lang-test.scm|true|true>
(check ($ 0 :sqrt) =\<gtr\> 0) \ \ \ \ \ \

(check ($ 1 :sqrt) =\<gtr\> 1) \ \ \ \ \ \

(check ($ 2 :sqrt) =\<gtr\> 1) \ \ \ \ \ \

(check ($ 9 :sqrt) =\<gtr\> 3) \ \ \ \ \ \

(check ($ 8 :sqrt) =\<gtr\> 2)

(check ($ 10 :sqrt) =\<gtr\> 3)

(check ($ 144 :sqrt) =\<gtr\> 12) \ \ \ \ \ \

(check ($ 289 :sqrt) =\<gtr\> 17) \ \ \ \ \ \

(check ($ 290 :sqrt) =\<gtr\> 17) \ \ \ \ \ \

(check ($ 10201 :sqrt) =\<gtr\> 101) \ \ \ \ \ \

(check ($ 10403 :sqrt) =\<gtr\> 101)\

(check ($ (rich-integer :max-value) :sqrt) =\<gtr\> 3037000499)

(check-catch 'value-error ($ -1 :sqrt))

\;
</goldfish-chunk>

<subsection|结尾>

<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
Expand Down
7 changes: 7 additions & 0 deletions goldfish/liii/lang.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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?))
Expand Down
14 changes: 14 additions & 0 deletions tests/goldfish/liii/lang-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down

0 comments on commit 9e232c7

Please sign in to comment.