6.3.90.900
3 Base Conversion Package
(require continued-fractions/bases) | |
package: continued-fractions |
3.1 Arbitrary Base Conversion
3.1.1 Configuration
The bindings in this section are not imported with continued-fractions.
parameter
(representation) → rep?
(representation v) → void? v : rep?
= decimal-representation
procedure
(make-representation [ #:radix rad #:negate neg #:terms ts]) → rep? rad : (or/c #f char?) = #\. neg : char? = #\- ts : string? = "0123456789"
This parameter controls the way string conversions happen in either direction.
It contains information
about which symbols to use for which numbers, what symbol to use as a radix point, and
which symbol to use as a negative sign. The magnitude of the base is determined from the
number of characters in the string specified by #:term. The first character of
the terms is considered to be zero,
the second character is considered to be one, and so on. The parameter is intended to be
constructed through make-representation. make-representation checks
that every part of the string is unique and checks that the radix and negate characters
differ from each other and the string. The radix can be #f, in which case it is
never displayed.
Controls the number of fractional digits produced.
3.1.2 Converting Continued Fractions
procedure
(representation-emit cf r) → sequence?
cf : continued-fraction? r : rep?
Emits terms based on the representation argument. Because the integer
part of a continued fraction doesn’t respect the base in base-emit, the integer
part is emitted as a base-converted list.
Example:
> (let ((binary (make-representation #:terms "01"))) (for/list ((t (representation-emit (cf- (phi-cf) 10) binary)) (i (in-range 10))) t)) '((#\- #\1 #\0 #\0 #\1) #\0 #\1 #\1 #\0 #\0 #\0 #\0 #\1 #\1)
procedure
cf : (or/c continued-fraction? (listof exact-integer?))
Converts a continued fraction, or a representation of one as a list of exact integers, into
a string based on the parameter representation.
Examples:
> (define p (make-representation #:radix #\_ #:negate #\$ #:terms "abcdefghij"))
> (parameterize ((representation p)) (continued-fraction->string (rational->cf 11/3))) "d_gggggggggg"
> (parameterize ((representation (make-representation #:terms "01")) (digits 20)) (continued-fraction->string (tangent-cf 157/50))) "-0.00000000011010000110"
> (parameterize ((digits 20)) (continued-fraction->string (phi-cf))) "1.61803398874989484820"
> (define ~phi (for/list ((t (base-emit (phi-cf) 10)) (i (in-range 40))) t))
> (continued-fraction->string ~phi) "1.618033988749894848204586834365638117720"
> (parameterize ((digits 20)) ; note this won't work because it interprets ; the terms in the continued fraction as a ; simple continued fraction, but this isn't ; a simple continued fraction, it's decimal (continued-fraction->string (base-emit (phi-cf) 10))) "1.14453286514339078255"
3.1.3 Converting Numbers and Strings
Converts an exact
number to a string based on representation. Trailing zeroes and leading
zeroes are not printed. If the argument is not an exact number, the expression
evaluates to #f.
Interprets a
string as a number based on representation. The parameter digits is
not respected when reading strings, only when writing them. If the argument is not
a string, the expression evaluates to #f.
Examples:
Converts a string with the current value of
representation to a number, then to a string based on the rep*
argument. The interpretation of the string doesn’t respect the digits
parameter, but the subsequent output of the string with the new representation
does.
Examples:
> (define alphabet* (make-representation #:radix #\, #:terms " abcdefghijklmnopqrstuvwxyz!"))
> (->number "123.456") 15432/125
> (string->string "123.456" alphabet*) "dk,lunccvqmfa"
> (string->string "123.456789011121314" (representation)) "123.4567890111"
> (parameterize ((digits 15)) (string->string "123.456789011121314" (representation))) "123.456789011121314"
> (parameterize ((representation alphabet*) (digits 0)) (->string 9395845/17210367)) " "
> (parameterize ((representation alphabet*) (digits 20)) (->string 263083660/481890303)) " ,oh my oh my oh my oh"
> (parameterize ((representation alphabet*) (digits 34)) (->string 1169629299742001994435343/232218265089212416)) "hello, racketeers!"