这个练习会用到莉娅的美味千层面中的整数运算。在这里,你将处理浮点数,并为具体任务挑选合适的除法词。
Factor 会区分整数(1、42、1_000_000)和浮点数(1.0、3.14、6.02e23)。如果在一次计算中混用整数和浮点数,结果就是浮点数:
2 3 + . ! => 5
2 3.0 + . ! => 5.0
你也可以在数字里加下划线来给数位分组,比如 1_000_000,Factor 会直接忽略这些下划线。
Factor 为除法提供了几个不同的词,它们都属于math词汇表。
/ ( x y -- x/y ) ! exact: ratio for two integers, float if either is a float
/f ( x y -- f ) ! always a float
/i ( x y -- q ) ! integer division (truncates toward zero)
mod ( x y -- r ) ! remainder
/mod ( x y -- q r ) ! quotient and remainder together
1 2 / . ! => 1/2 (a proper ratio)
16 3 / . ! => 5+1/3 (an improper ratio, printed as a mixed numeral)
16 3 /f . ! => 5.333333333333333
16 3 /i . ! => 5 (truncated)
16 3 mod . ! => 1
16 3 /mod .s ! quotient and remainder in one pass
! => 5
! => 1
16.0 3 / . ! => 5.333333333333333
对两个整数来说,/得到的是ratio(Factor 的精确有理数类型);只要有一个输入是浮点数,结果就是float。/f即使两个输入都是整数,也会强制返回浮点数结果。/i始终向零截断。
来自math、math.functions和math.primes的数字判定词:
zero? ( x -- ? )
even? ( x -- ? )
odd? ( x -- ? )
prime? ( x -- ? )
divisor? ( m n -- ? ) ! true when n divides m
integer? ( x -- ? ) ! true for ints (fixnum or bignum)
float? ( x -- ? ) ! true for floats
number? ( x -- ? ) ! true for any numeric kind
0 zero? . ! => t
0.0 zero? . ! => t
3 zero? . ! => f
4 even? . ! => t
7 even? . ! => f
7 odd? . ! => t
7 prime? . ! => t
9 prime? . ! => f
15 5 divisor? . ! => t (5 divides 15)
15 4 divisor? . ! => f
math.functions词汇表里的floor、ceiling和round都返回浮点数(3.0,而不是3)。要得到真正的整数,可以接着调用>integer:
3.7 floor >integer . ! => 3
3.2 ceiling >integer . ! => 4
sq、^和abs(在math.functions中)分别用来计算平方、任意次幂和绝对值:
sq ( x -- x*x )
^ ( x y -- x^y )
abs ( x -- |x| )
5 sq . ! => 25
2 10 ^ . ! => 1024
2 0.5 ^ . ! => 1.4142135623730951
-7 abs . ! => 7
3 abs . ! => 3
min ( x y -- z )和max ( x y -- z )(在math.order中)返回两个数中较小或较大的那个:
3 7 min . ! => 3
3 7 max . ! => 7
最常见的用法是钳制,也就是用max把一个值固定在下界(例如0 max能让值保持非负),或者用min把它固定在上界。
math.order还提供了before?、after?、before=?和after=?,这些是比较判定词,适用于任何可排序的类型:数字、字符串和字符:
before? ( obj1 obj2 -- ? ) ! obj1 < obj2
before=? ( obj1 obj2 -- ? ) ! obj1 <= obj2
after? ( obj1 obj2 -- ? ) ! obj1 > obj2
after=? ( obj1 obj2 -- ? ) ! obj1 >= obj2
当值可能是字符串时,请用这些判定词代替</<=,因为算术运算符不会对字符串进行分派。
你的朋友 Chandler 打算去世界各地充满异国情调的国家旅行。可惜的是,Chandler 的数学不太好。他很担心在旅途中被货币兑换处骗,所以想让你帮他做一个货币计算器。
定义exchange-money,它接收budget和exchange-rate,并返回兑换后货币的价值。
**注意:**如果你的货币是 USD,想按1.20的汇率把 USD 兑换成 EUR,那么1.20 USD == 1 EUR。
127.5 1.2 exchange-money .
! => 106.25
定义get-change,它接收budget(兑换前的预算)和exchanging-value(从预算中取出用于兑换的金额)。返回剩余的钱数。
127.5 120 get-change .
! => 7.5
定义value-of-bills,它接收denomination(单张纸币的价值)和number-of-bills。返回这些纸币的总价值。
5 128 value-of-bills .
! => 640
定义number-of-bills,它接收amount和denomination。返回该金额中能容纳多少张给定面额的整张纸币。向下取整,因为不存在零碎的纸币。
127.5 5 number-of-bills .
! => 25
定义leftover-of-bills,它接收amount和denomination。返回无法以整张纸币形式换回的那部分剩余金额。
127.5 20 leftover-of-bills .
! => 7.5
定义exchangeable-value,它接收denomination、budget、spread和exchange-rate。
spread是以整数形式表示的百分比兑换手续费。它需要以分数的形式加到汇率上。如果1.00 EUR == 1.20 USD,且spread为10,那么总汇率就是1.00 EUR == 1.32 USD(1.20 的 10% 是 0.12,加到 1.20 上)。
返回在汇率加上 spread 之后新货币的最大价值,向下取整为给定denomination的整张纸币。返回的值是一个整数。
20 127.25 10 1.20 exchangeable-value .
! => 80
5 127.25 10 1.20 exchangeable-value .
! => 95
和get-change类似,但如果 Chandler 不小心输入的兑换金额大于他的预算,就返回0,而不是负数。定义safe-change,它接收budget和exchanging-value。
127.5 120 safe-change .
! => 7.5
50 100 safe-change .
! => 0
Chandler 正在为一件纪念品讨价还价。定义cap-spend,它接收budget和price,并返回两者中较小的那个,也就是他实际能支付的最高金额。
100 30 cap-spend .
! => 30
20 30 cap-spend .
! => 20