7. Summary
Everything you should know from Chapter 7.
The chapter is about doing math with numbers.
Essential summary
- Basic arithmetic
(+ 5 3)
(/ 13 7) ; thirteen divided by seven
(* 11 2)
(- 9 1) ; nine minus one
(- 8)
(quotient 21912 7)
(remainder 21912 7)
- Maximum, minimum, absolute value
(max 0 -4) ; ==> 0
(min 255 300) ; ==> 255
(abs -10) ; ==> 10
- Exponents, square root, squaring:
(expt 1.01 95) ; 1.01 to the 95 power is about 2.57
(sqrt 9) ; square root of 9 is three
(sqr 1241) ; when you square 1241 you get 1540081
- Inexact answers, checking with tolerance
> (sqrt 5) ; about 2.236
#i2.23606797749979
The #i
at the start of #i2.236...
means that the number is an “inexact” decimal.
You should never use check-expect with inexact numbers, because the inexact results can be different on different computers! Instead, use check-within
.
(check-within (sqrt 5) 2.236 0.001)
- Special numbers: both pi is built in. Your tests should still pass if you use either the buit in value of
pi
or3.1415
.
> pi
#i3.141592653589793
- Getting rid of fractions. The best generic method to use is
real->int
. Otherwise there are specific functions that will round traditionally, round up (ceiling), and round down (floor).
(real->int 0.5)
(real->int 1.5)
(real->int 2.5)
(ceiling 1.2)
(floor 1.7)
(round 1.6)
More Important Ideas
- Inexact numbers and
check-within
build3-image
orbuild-image
map3-image
ormap-image
(random 5)
gives 0, 1, 2, 3, or 4.- A remainder of zero means the numerator is a multiple of the denominator.
- How to make a gradient, changing color smoothly from pink to light blue.