fenv.nim 9.4 KB

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