/core/math/floats/floats-docs.factor

http://github.com/abeaumont/factor · Factor · 163 lines · 139 code · 23 blank · 1 comment · 0 complexity · 211e10284aeabbcc1718d78a5e320d7c MD5 · raw file

  1. USING: help.markup help.syntax math math.private ;
  2. IN: math.floats
  3. HELP: float
  4. { $class-description "The class of double-precision floating point numbers." } ;
  5. HELP: >float
  6. { $values { "x" real } { "y" float } }
  7. { $description "Converts a real to a float. This is the identity on floats, and performs a floating point division on rationals." } ;
  8. HELP: bits>double
  9. { $values { "n" "a 64-bit integer representing an IEEE 754 double-precision float" } { "x" float } }
  10. { $description "Creates a " { $link float } " object from a 64-bit binary representation. This word is usually used to reconstruct floats read from streams." } ;
  11. { bits>double bits>float double>bits float>bits } related-words
  12. HELP: bits>float
  13. { $values { "n" "a 32-bit integer representing an IEEE 754 single-precision float" } { "x" float } }
  14. { $description "Creates a " { $link float } " object from a 32-bit binary representation. This word is usually used to reconstruct floats read from streams." } ;
  15. HELP: double>bits
  16. { $values { "x" float } { "n" "a 64-bit integer representing an IEEE 754 double-precision float" } }
  17. { $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." } ;
  18. HELP: float>bits
  19. { $values { "x" float } { "n" "a 32-bit integer representing an IEEE 754 single-precision float" } }
  20. { $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." } ;
  21. ! Unsafe primitives
  22. HELP: float+
  23. { $values { "x" float } { "y" float } { "z" float } }
  24. { $description "Primitive version of " { $link + } "." }
  25. { $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." } ;
  26. HELP: float-
  27. { $values { "x" float } { "y" float } { "z" float } }
  28. { $description "Primitive version of " { $link - } "." }
  29. { $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." } ;
  30. HELP: float*
  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. HELP: float/f
  35. { $values { "x" float } { "y" float } { "z" float } }
  36. { $description "Primitive version of " { $link /f } "." }
  37. { $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." } ;
  38. HELP: float<
  39. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  40. { $description "Primitive version of " { $link < } "." }
  41. { $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." } ;
  42. HELP: float<=
  43. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  44. { $description "Primitive version of " { $link <= } "." }
  45. { $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." } ;
  46. HELP: float>
  47. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  48. { $description "Primitive version of " { $link > } "." }
  49. { $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." } ;
  50. HELP: float>=
  51. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  52. { $description "Primitive version of " { $link u>= } "." }
  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 u>= } " instead." } ;
  54. HELP: float-u<
  55. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  56. { $description "Primitive version of " { $link u< } "." }
  57. { $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." } ;
  58. HELP: float-u<=
  59. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  60. { $description "Primitive version of " { $link u<= } "." }
  61. { $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." } ;
  62. HELP: float-u>
  63. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  64. { $description "Primitive version of " { $link u> } "." }
  65. { $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." } ;
  66. HELP: float-u>=
  67. { $values { "x" float } { "y" float } { "?" "a boolean" } }
  68. { $description "Primitive version of " { $link u>= } "." }
  69. { $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." } ;
  70. ARTICLE: "math.floats.compare" "Floating point comparison operations"
  71. "In mathematics, real numbers are linearly ordered; for any two numbers " { $snippet "a" } " and " { $snippet "b" } ", exactly one of the following is true:"
  72. { $code
  73. "a < b"
  74. "a = b"
  75. "a > b"
  76. }
  77. "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."
  78. $nl
  79. "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)."
  80. $nl
  81. "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."
  82. $nl
  83. "The " { $link number= } " operation performs an unordered comparison. The following set of operators also perform unordered comparisons:"
  84. { $subsections
  85. u<
  86. u<=
  87. u>
  88. u>=
  89. }
  90. "A word to check if two values are unordered with respect to each other:"
  91. { $subsections unordered? }
  92. "To test for floating point exceptions, use the " { $vocab-link "math.floats.env" } " vocabulary."
  93. $nl
  94. "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." ;
  95. ARTICLE: "math.floats.bitwise" "Bitwise operations on floats"
  96. "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."
  97. { $subsections
  98. float>bits
  99. double>bits
  100. bits>float
  101. bits>double
  102. }
  103. "Constructing floating point NaNs:"
  104. { $subsections <fp-nan> }
  105. "Floating point numbers are discrete:"
  106. { $subsections
  107. prev-float
  108. next-float
  109. }
  110. "Introspection on floating point numbers:"
  111. { $subsections
  112. fp-special?
  113. fp-nan?
  114. fp-qnan?
  115. fp-snan?
  116. fp-infinity?
  117. fp-nan-payload
  118. }
  119. "Comparing two floating point numbers for bitwise equality:"
  120. { $subsections fp-bitwise= }
  121. { $see-also POSTPONE: NAN: } ;
  122. ARTICLE: "floats" "Floats"
  123. { $subsections float }
  124. "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."
  125. $nl
  126. "Introducing a floating point number in a computation forces the result to be expressed in floating point."
  127. { $example "5/4 1/2 + ." "1+3/4" }
  128. { $example "5/4 0.5 + ." "1.75" }
  129. "Floating point literal syntax is documented in " { $link "syntax-floats" } "."
  130. $nl
  131. "Integers and rationals can be converted to floats:"
  132. { $subsections >float }
  133. "Two real numbers can be divided yielding a float result:"
  134. { $subsections
  135. /f
  136. "math.floats.bitwise"
  137. "math.floats.compare"
  138. }
  139. "The " { $vocab-link "math.floats.env" } " vocabulary provides functionality for controlling floating point exceptions, rounding modes, and denormal behavior." ;
  140. ABOUT: "floats"