]> gitweb.factorcode.org Git - factor.git/blob - core/math/floats/floats-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor into constraints
[factor.git] / core / math / floats / floats-docs.factor
1 USING: help.markup help.syntax math math.private ;
2 IN: math.floats
3
4 HELP: float
5 { $class-description "The class of double-precision floating point numbers." } ;
6
7 HELP: >float
8 { $values { "x" real } { "y" float } }
9 { $description "Converts a real to a float. This is the identity on floats, and performs a floating point division on rationals." } ;
10
11 HELP: bits>double ( n -- x )
12 { $values { "n" "a 64-bit integer representing an IEEE 754 double-precision float" } { "x" float } }
13 { $description "Creates a " { $link float } " object from a 64-bit binary representation. This word is usually used to reconstruct floats read from streams." } ;
14
15 { bits>double bits>float double>bits float>bits } related-words
16
17 HELP: bits>float ( n -- x )
18 { $values { "n" "a 32-bit integer representing an IEEE 754 single-precision float" } { "x" float } }
19 { $description "Creates a " { $link float } " object from a 32-bit binary representation. This word is usually used to reconstruct floats read from streams." } ;
20
21 HELP: double>bits ( x -- n )
22 { $values { "x" float } { "n" "a 64-bit integer representing an IEEE 754 double-precision float" } }
23 { $description "Creates a 64-bit binary representation of a " { $link float } " object. This can be used in the process of writing a float to a stream." } ;
24
25 HELP: float>bits ( x -- n )
26 { $values { "x" float } { "n" "a 32-bit integer representing an IEEE 754 single-precision float" } }
27 { $description "Creates a 32-bit binary representation of a " { $link float } " object. This can be used in the process of writing a float to a stream." } ;
28
29 ! Unsafe primitives
30 HELP: float+ ( x y -- z )
31 { $values { "x" float } { "y" float } { "z" float } }
32 { $description "Primitive version of " { $link + } "." }
33 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link + } " instead." } ;
34
35 HELP: float- ( x y -- z )
36 { $values { "x" float } { "y" float } { "z" float } }
37 { $description "Primitive version of " { $link - } "." }
38 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link - } " instead." } ;
39
40 HELP: float* ( x y -- z )
41 { $values { "x" float } { "y" float } { "z" float } }
42 { $description "Primitive version of " { $link * } "." }
43 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link * } " instead." } ;
44
45 HELP: float-mod ( x y -- z )
46 { $values { "x" float } { "y" float } { "z" float } }
47 { $description "Primitive version of " { $link mod } "." }
48 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link mod } " instead." } ;
49
50 HELP: float/f ( x y -- z )
51 { $values { "x" float } { "y" float } { "z" float } }
52 { $description "Primitive version of " { $link /f } "." }
53 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link /f } " instead." } ;
54
55 HELP: float< ( x y -- ? )
56 { $values { "x" float } { "y" float } { "?" "a boolean" } }
57 { $description "Primitive version of " { $link < } "." }
58 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link < } " instead." } ;
59
60 HELP: float<= ( x y -- ? )
61 { $values { "x" float } { "y" float } { "?" "a boolean" } }
62 { $description "Primitive version of " { $link <= } "." }
63 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link <= } " instead." } ;
64
65 HELP: float> ( x y -- ? )
66 { $values { "x" float } { "y" float } { "?" "a boolean" } }
67 { $description "Primitive version of " { $link > } "." }
68 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link > } " instead." } ;
69
70 HELP: float>= ( x y -- ? )
71 { $values { "x" float } { "y" float } { "?" "a boolean" } }
72 { $description "Primitive version of " { $link u>= } "." }
73 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u>= } " instead." } ;
74
75 HELP: float-u< ( x y -- ? )
76 { $values { "x" float } { "y" float } { "?" "a boolean" } }
77 { $description "Primitive version of " { $link u< } "." }
78 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u< } " instead." } ;
79
80 HELP: float-u<= ( x y -- ? )
81 { $values { "x" float } { "y" float } { "?" "a boolean" } }
82 { $description "Primitive version of " { $link u<= } "." }
83 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u<= } " instead." } ;
84
85 HELP: float-u> ( x y -- ? )
86 { $values { "x" float } { "y" float } { "?" "a boolean" } }
87 { $description "Primitive version of " { $link u> } "." }
88 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u> } " instead." } ;
89
90 HELP: float-u>= ( x y -- ? )
91 { $values { "x" float } { "y" float } { "?" "a boolean" } }
92 { $description "Primitive version of " { $link u>= } "." }
93 { $warning "This word does not perform type checking, and passing objects of the wrong type can crash the runtime. User code should call the generic word " { $link u>= } " instead." } ;
94
95 ARTICLE: "math.floats.compare" "Floating point comparison operations"
96 "In mathematics, real numbers are linearly ordered; for any two numbers " { $snippet "a" } " and " { $snippet "b" } ", exactly one of the following is true:"
97 { $code
98     "a < b"
99     "a = b"
100     "a > b"
101 }
102 "With floating point values, there is a fourth possibility; " { $snippet "a" } " and " { $snippet "b" } " may be " { $emphasis "unordered" } ". This happens if one or both values are Not-a-Number values."
103 $nl
104 "All comparison operators, including " { $link number= } ", return " { $link f } " in the unordered case (and in particular, this means that a NaN is not equal to itself)."
105 $nl
106 "The " { $emphasis "ordered" } " comparison operators set floating point exception flags if the result of the comparison is unordered. The standard comparison operators (" { $link < } ", " { $link <= } ", " { $link > } ", " { $link >= } ") perform ordered comparisons."
107 $nl
108 "The " { $link number= } " operation performs an unordered comparison. The following set of operators also perform unordered comparisons:"
109 { $subsection u< }
110 { $subsection u<= }
111 { $subsection u> }
112 { $subsection u>= }
113 "A word to check if two values are unordered with respect to each other:"
114 { $subsection unordered? }
115 "To test for floating point exceptions, use the " { $vocab-link "math.floats.env" } " vocabulary."
116 $nl
117 "If neither input to a comparison operator is a floating point value, then " { $link u< } ", " { $link u<= } ", " { $link u> } " and " { $link u>= } " are equivalent to the ordered operators." ;
118
119 ARTICLE: "math.floats.bitwise" "Bitwise operations on floats"
120 "Floating point numbers are represented internally in IEEE 754 double-precision format. This internal representation can be accessed for advanced operations and input/output purposes."
121 { $subsection float>bits }
122 { $subsection double>bits }
123 { $subsection bits>float }
124 { $subsection bits>double }
125 "Constructing floating point NaNs:"
126 { $subsection <fp-nan> }
127 "Floating point numbers are discrete:"
128 { $subsection prev-float }
129 { $subsection next-float }
130 "Introspection on floating point numbers:"
131 { $subsection fp-special? }
132 { $subsection fp-nan? }
133 { $subsection fp-qnan? }
134 { $subsection fp-snan? }
135 { $subsection fp-infinity? }
136 { $subsection fp-nan-payload }
137 "Comparing two floating point numbers for bitwise equality:"
138 { $subsection fp-bitwise= }
139 { $see-also POSTPONE: NAN: } ;
140
141 ARTICLE: "floats" "Floats"
142 { $subsection float }
143 "Rational numbers represent " { $emphasis "exact" } " quantities. On the other hand, a floating point number is an " { $emphasis "approximate" } " value. While rationals can grow to any required precision, floating point numbers have limited precision, and manipulating them is usually faster than manipulating ratios or bignums."
144 $nl
145 "Introducing a floating point number in a computation forces the result to be expressed in floating point."
146 { $example "5/4 1/2 + ." "1+3/4" }
147 { $example "5/4 0.5 + ." "1.75" }
148 "Floating point literal syntax is documented in " { $link "syntax-floats" } "."
149 $nl
150 "Integers and rationals can be converted to floats:"
151 { $subsection >float }
152 "Two real numbers can be divided yielding a float result:"
153 { $subsection /f }
154 { $subsection "math.floats.bitwise" }
155 { $subsection "math.floats.compare" }
156 "The " { $vocab-link "math.floats.env" } " vocabulary provides functionality for controlling floating point exceptions, rounding modes, and denormal behavior." ;
157
158 ABOUT: "floats"