fenv.nim 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Floating-point environment. Handling of floating-point rounding and
  10. ## exceptions (overflow, division by zero, etc.).
  11. ## The types, vars and procs are bindings for the C standard library
  12. ## [<fenv.h>](https://en.cppreference.com/w/c/numeric/fenv) header.
  13. when defined(posix) and not defined(genode):
  14. {.passl: "-lm".}
  15. var
  16. FE_DIVBYZERO* {.importc, header: "<fenv.h>".}: cint
  17. ## division by zero
  18. FE_INEXACT* {.importc, header: "<fenv.h>".}: cint
  19. ## inexact result
  20. FE_INVALID* {.importc, header: "<fenv.h>".}: cint
  21. ## invalid operation
  22. FE_OVERFLOW* {.importc, header: "<fenv.h>".}: cint
  23. ## result not representable due to overflow
  24. FE_UNDERFLOW* {.importc, header: "<fenv.h>".}: cint
  25. ## result not representable due to underflow
  26. FE_ALL_EXCEPT* {.importc, header: "<fenv.h>".}: cint
  27. ## bitwise OR of all supported exceptions
  28. FE_DOWNWARD* {.importc, header: "<fenv.h>".}: cint
  29. ## round toward -Inf
  30. FE_TONEAREST* {.importc, header: "<fenv.h>".}: cint
  31. ## round to nearest
  32. FE_TOWARDZERO* {.importc, header: "<fenv.h>".}: cint
  33. ## round toward 0
  34. FE_UPWARD* {.importc, header: "<fenv.h>".}: cint
  35. ## round toward +Inf
  36. FE_DFL_ENV* {.importc, header: "<fenv.h>".}: cint
  37. ## macro of type pointer to `fenv_t` to be used as the argument
  38. ## to functions taking an argument of type `fenv_t`; in this
  39. ## case the default environment will be used
  40. type
  41. Tfenv* {.importc: "fenv_t", header: "<fenv.h>", final, pure.} =
  42. object ## Represents the entire floating-point environment. The
  43. ## floating-point environment refers collectively to any
  44. ## floating-point status flags and control modes supported
  45. ## by the implementation.
  46. Tfexcept* {.importc: "fexcept_t", header: "<fenv.h>", final, pure.} =
  47. object ## Represents the floating-point status flags collectively,
  48. ## including any status the implementation associates with the
  49. ## flags. A floating-point status flag is a system variable
  50. ## whose value is set (but never cleared) when a floating-point
  51. ## exception is raised, which occurs as a side effect of
  52. ## exceptional floating-point arithmetic to provide auxiliary
  53. ## information. A floating-point control mode is a system variable
  54. ## whose value may be set by the user to affect the subsequent
  55. ## behavior of floating-point arithmetic.
  56. proc feclearexcept*(excepts: cint): cint {.importc, header: "<fenv.h>".}
  57. ## Clear the supported exceptions represented by `excepts`.
  58. proc fegetexceptflag*(flagp: ptr Tfexcept, excepts: cint): cint {.
  59. importc, header: "<fenv.h>".}
  60. ## Store implementation-defined representation of the exception flags
  61. ## indicated by `excepts` in the object pointed to by `flagp`.
  62. proc feraiseexcept*(excepts: cint): cint {.importc, header: "<fenv.h>".}
  63. ## Raise the supported exceptions represented by `excepts`.
  64. proc fesetexceptflag*(flagp: ptr Tfexcept, excepts: cint): cint {.
  65. importc, header: "<fenv.h>".}
  66. ## Set complete status for exceptions indicated by `excepts` according to
  67. ## the representation in the object pointed to by `flagp`.
  68. proc fetestexcept*(excepts: cint): cint {.importc, header: "<fenv.h>".}
  69. ## Determine which of subset of the exceptions specified by `excepts` are
  70. ## currently set.
  71. proc fegetround*(): cint {.importc, header: "<fenv.h>".}
  72. ## Get current rounding direction.
  73. proc fesetround*(roundingDirection: cint): cint {.importc, header: "<fenv.h>".}
  74. ## Establish the rounding direction represented by `roundingDirection`.
  75. proc fegetenv*(envp: ptr Tfenv): cint {.importc, header: "<fenv.h>".}
  76. ## Store the current floating-point environment in the object pointed
  77. ## to by `envp`.
  78. proc feholdexcept*(envp: ptr Tfenv): cint {.importc, header: "<fenv.h>".}
  79. ## Save the current environment in the object pointed to by `envp`, clear
  80. ## exception flags and install a non-stop mode (if available) for all
  81. ## exceptions.
  82. proc fesetenv*(a1: ptr Tfenv): cint {.importc, header: "<fenv.h>".}
  83. ## Establish the floating-point environment represented by the object
  84. ## pointed to by `envp`.
  85. proc feupdateenv*(envp: ptr Tfenv): cint {.importc, header: "<fenv.h>".}
  86. ## Save current exceptions in temporary storage, install environment
  87. ## represented by object pointed to by `envp` and raise exceptions
  88. ## according to saved exceptions.
  89. const
  90. FLT_RADIX = 2 ## the radix of the exponent representation
  91. FLT_MANT_DIG = 24 ## the number of base FLT_RADIX digits in the mantissa part of a float
  92. FLT_DIG = 6 ## the number of digits of precision of a float
  93. FLT_MIN_EXP = -125 ## the minimum value of base FLT_RADIX in the exponent part of a float
  94. FLT_MAX_EXP = 128 ## the maximum value of base FLT_RADIX in the exponent part of a float
  95. FLT_MIN_10_EXP = -37 ## the minimum value in base 10 of the exponent part of a float
  96. FLT_MAX_10_EXP = 38 ## the maximum value in base 10 of the exponent part of a float
  97. FLT_MIN = 1.17549435e-38'f32 ## the minimum value of a float
  98. FLT_MAX = 3.40282347e+38'f32 ## the maximum value of a float
  99. FLT_EPSILON = 1.19209290e-07'f32 ## the difference between 1 and the least value greater than 1 of a float
  100. DBL_MANT_DIG = 53 ## the number of base FLT_RADIX digits in the mantissa part of a double
  101. DBL_DIG = 15 ## the number of digits of precision of a double
  102. DBL_MIN_EXP = -1021 ## the minimum value of base FLT_RADIX in the exponent part of a double
  103. DBL_MAX_EXP = 1024 ## the maximum value of base FLT_RADIX in the exponent part of a double
  104. DBL_MIN_10_EXP = -307 ## the minimum value in base 10 of the exponent part of a double
  105. DBL_MAX_10_EXP = 308 ## the maximum value in base 10 of the exponent part of a double
  106. DBL_MIN = 2.2250738585072014E-308 ## the minimal value of a double
  107. DBL_MAX = 1.7976931348623157E+308 ## the minimal value of a double
  108. DBL_EPSILON = 2.2204460492503131E-16 ## the difference between 1 and the least value greater than 1 of a double
  109. template fpRadix*: int = FLT_RADIX
  110. ## The (integer) value of the radix used to represent any floating
  111. ## point type on the architecture used to build the program.
  112. template mantissaDigits*(T: typedesc[float32]): int = FLT_MANT_DIG
  113. ## Number of digits (in base `floatingPointRadix`) in the mantissa
  114. ## of 32-bit floating-point numbers.
  115. template digits*(T: typedesc[float32]): int = FLT_DIG
  116. ## Number of decimal digits that can be represented in a
  117. ## 32-bit floating-point type without losing precision.
  118. template minExponent*(T: typedesc[float32]): int = FLT_MIN_EXP
  119. ## Minimum (negative) exponent for 32-bit floating-point numbers.
  120. template maxExponent*(T: typedesc[float32]): int = FLT_MAX_EXP
  121. ## Maximum (positive) exponent for 32-bit floating-point numbers.
  122. template min10Exponent*(T: typedesc[float32]): int = FLT_MIN_10_EXP
  123. ## Minimum (negative) exponent in base 10 for 32-bit floating-point
  124. ## numbers.
  125. template max10Exponent*(T: typedesc[float32]): int = FLT_MAX_10_EXP
  126. ## Maximum (positive) exponent in base 10 for 32-bit floating-point
  127. ## numbers.
  128. template minimumPositiveValue*(T: typedesc[float32]): float32 = FLT_MIN
  129. ## The smallest positive (nonzero) number that can be represented in a
  130. ## 32-bit floating-point type.
  131. template maximumPositiveValue*(T: typedesc[float32]): float32 = FLT_MAX
  132. ## The largest positive number that can be represented in a 32-bit
  133. ## floating-point type.
  134. template epsilon*(T: typedesc[float32]): float32 = FLT_EPSILON
  135. ## The difference between 1.0 and the smallest number greater than
  136. ## 1.0 that can be represented in a 32-bit floating-point type.
  137. template mantissaDigits*(T: typedesc[float64]): int = DBL_MANT_DIG
  138. ## Number of digits (in base `floatingPointRadix`) in the mantissa
  139. ## of 64-bit floating-point numbers.
  140. template digits*(T: typedesc[float64]): int = DBL_DIG
  141. ## Number of decimal digits that can be represented in a
  142. ## 64-bit floating-point type without losing precision.
  143. template minExponent*(T: typedesc[float64]): int = DBL_MIN_EXP
  144. ## Minimum (negative) exponent for 64-bit floating-point numbers.
  145. template maxExponent*(T: typedesc[float64]): int = DBL_MAX_EXP
  146. ## Maximum (positive) exponent for 64-bit floating-point numbers.
  147. template min10Exponent*(T: typedesc[float64]): int = DBL_MIN_10_EXP
  148. ## Minimum (negative) exponent in base 10 for 64-bit floating-point
  149. ## numbers.
  150. template max10Exponent*(T: typedesc[float64]): int = DBL_MAX_10_EXP
  151. ## Maximum (positive) exponent in base 10 for 64-bit floating-point
  152. ## numbers.
  153. template minimumPositiveValue*(T: typedesc[float64]): float64 = DBL_MIN
  154. ## The smallest positive (nonzero) number that can be represented in a
  155. ## 64-bit floating-point type.
  156. template maximumPositiveValue*(T: typedesc[float64]): float64 = DBL_MAX
  157. ## The largest positive number that can be represented in a 64-bit
  158. ## floating-point type.
  159. template epsilon*(T: typedesc[float64]): float64 = DBL_EPSILON
  160. ## The difference between 1.0 and the smallest number greater than
  161. ## 1.0 that can be represented in a 64-bit floating-point type.