2. Numbers

Checking Whether a String Is a Valid Number

Comparing Floating-Point Numbers

Rounding Floating-Point Numbers

Converting Between Binary and Decimal

Operating on a Series of Integers

print("Infancy is: ") ; 0..2.map(e -> print("{e} "))
print("Infancy is: ") ; 0..2.map(to_string).join(" ").print

Working with Roman Numerals

Generating Random Numbers

rand = random(25 .. 75)                 # rand is in [25,75[

chars = "A".."Z" + "a".."z" + "0".."9" + q(!@$%^&*)
password = 1..8.map(_ -> chars[random(chars.size)]).flatten

Generating Different Random Numbers

Making Numbers Even More Random

Generating Biased Random Numbers

Doing Trigonometry in Degrees, not Radians

Calculating More Trigonometric Functions

Taking Logarithms

Multiplying Matrices

Using Complex Numbers

Converting Between Octal and Hexadecimal

number = hex(hexadecimal) # hexadecimal
number = to_int(octal, 8) # octal

print("Gimme a number in decimal, octal, or hex: ")
num = Sys::stdin.line.chomp
num = num.pattern_matches(
    "^0x", hex(num),
    "^0", to_string(num, 8),
    "", num,
)
"%d %x %o\n".printf(num, num, num)

Putting Commas in Numbers

s.commify =
    integer.fixpoint(subst(, "(\d)(\d\{3})($|\.|,)", (a,b,c -> "{a},{b}{c}")))
s.commify' =
    s.pattern_matches(
        "(.*)(...)", (a,b -> a.commify' + "," + b)
        "", s
    )
    

Printing Correct Plurals

Program: Calculating Prime Factors