Skip to content

Floating-point number

Check a number class of floating point numbers

Section titled “Check a number class of floating point numbers”

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (<predicate> <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

predicate value output
number? 3.14 A
number? -42.2045 A
complex? 3.14 A
complex? -42.2045 A
real? 3.14 A
real? -42.2045 A
rational? 0 A
rational? 1 A
rational? -42 A
rational? 3.14 A
rational? -42.2045 A
rational? (exp 1) A
integer? 3.14 B
integer? -42.2045 B

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (exact? <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

value output
-42 A
-3.14 B
0 A
3.14 B
42 A

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (inexact? <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

value output
-42 B
-3.14 A
0 B
3.14 A
42 B

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (exact-integer? <value>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

value output
-42 A
-3.14 B
0 A
3.14 B
42 A

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (< (abs (- (expt 2 3) (exp (* (log 2) 3)))) 0.000001) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (< (abs (- (log 2 3) (/ (log 2) (log 3)))) 0.000001) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (= (truncate <input>) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
0 0
0.1 0
0.9 0
1 1
1.1 1
1.9 1
2 2
-0.9 0
-1 -1
-1.9 -1
-2 -2
-2.1 -2

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (= (floor <input>) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
0 0
0.1 0
0.9 0
1 1
1.1 1
1.9 1
2 2
-0.9 -1
-1 -1
-1.9 -2
-2 -2
-2.1 -3

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (= (ceiling <input>) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
0 0
0.1 1
0.9 1
1 1
1.1 2
1.9 2
2 2
-0.9 0
-1 -1
-1.9 -1
-2 -2
-2.1 -2

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (= (round <input>) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
0 0
0.1 0
0.49 0
0.5 0
0.9 1
1 1
1.1 1
1.49 1
1.5 2
1.9 2
2 2
2.5 2
3.5 4
-1 -1
-0.9 -1
-1.5 -2
-1.51 -2
-1.9 -2

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8 (if (= (rationalize <input>) <output>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input output
0 0 0
42 0 42
42 (/ 1 10) 42
0.1 0 0.1

Convert a floating point number to a string

Section titled “Convert a floating point number to a string”

Given a file named “main.scm” with:

(import (scheme base))
(write-string (number->string <value>))

When I successfully run stak main.scm

Then the stdout should contain “<value>”.

value
0.5
0.125
1.2
3.14
-3.14

Convert a non-finite floating point number to a string

Section titled “Convert a non-finite floating point number to a string”

Given a file named “main.scm” with:

(import (scheme base))
(write-string (number->string <value>))

When I successfully run stak main.scm

Then the stdout should contain “<output>”.

value output
(/ 0 0) nan
(/ 1 0) infinity
(/ -1 0) -infinity

Convert a string to a floating point number

Section titled “Convert a string to a floating point number”

Given a file named “main.scm” with:

(import (scheme base))
(write-string (number->string (string->number "<value>")))

When I successfully run stak main.scm

Then the stdout should contain exactly “<value>”.

value
0.5
0.125
1.2
3.14
-3.14

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8
(if (< (abs (- (expt (sqrt <value>) 2) <value>)) 0.001)
65
66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

value
0
1
2
42

Given a file named “main.scm” with:

(import (scheme base) (scheme inexact))
(write-u8
(if (< (abs (- (/ (sin <value>) (cos <value>)) (tan <value>))) 0.001)
65
66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

value
-2
-1
0
1
2

Given a file named “main.scm” with:

(import (scheme base) (scheme complex) (scheme inexact))
(write-u8
(if (= (round (real-part (<normal> (<inverse> 1)))) 1)
65
66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

normal inverse
cos acos
sin asin
tan atan