Module: FPU
- Defined in:
- lib/fpu.rb,
ext/fpu.c
Overview
This module provides the way of controlling the rounding mode of the floating-point unit (FPU) and also provides mathematical functions that round their result consistently with the specified rounding mode.
Constant Summary collapse
- StandardRounding =
:stopdoc:
rounding
- PlusRounding =
:nodoc:
up { rounding }
- MinusRounding =
:nodoc:
down{ rounding }
Class Method Summary collapse
-
.down ⇒ Object
Perform a computation with the FPU rounding downwards.
-
.power(x, n) ⇒ Object
Raise
x
to then
-th power with correct rounding. -
.rounding ⇒ Object
Get the FPU’s rounding mode.
-
.rounding=(mode) ⇒ Object
Set the FPU’s rounding mode.
-
.roundingDown ⇒ Object
Enable the FPU’s downward rounding.
-
.roundingUp ⇒ Object
Enable the FPU’s upward rounding.
-
.up ⇒ Object
Perform a computation with the FPU rounding upwards.
Class Method Details
.down ⇒ Object
42 43 44 45 46 47 |
# File 'lib/fpu.rb', line 42 def FPU.down saved = roundingDown yield ensure self.rounding = saved end |
.power(x, n) ⇒ Object
Raise x
to the n
-th power with correct rounding.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fpu.rb', line 63 def FPU.power(x, n) if Float === x && Integer === n return 1/FPU.power(x,-n) if n < 0 l=[]; while n > 0 n,y=n.divmod(2); l=[y,l] end l.flatten.inject(1){|p,i| if i == 1 then p*p*x; else p*p; end} else x ** n end end |
.rounding ⇒ Object
Get the FPU’s rounding mode.
20 21 22 23 |
# File 'ext/fpu.c', line 20
static VALUE mFPU_rounding(VALUE self)
{
return UINT2NUM(fegetround());
}
|
.rounding=(mode) ⇒ Object
Set the FPU’s rounding mode.
28 29 30 31 32 |
# File 'ext/fpu.c', line 28
static VALUE mFPU_rounding_set(VALUE self, VALUE mode)
{
fesetround(NUM2UINT(mode));
return UINT2NUM(fegetround());
}
|
.roundingDown ⇒ Object
Enable the FPU’s downward rounding.
47 48 49 50 51 52 |
# File 'ext/fpu.c', line 47
static VALUE mFPU_rounding_down(VALUE self)
{
int saved = fegetround();
fesetround(FE_DOWNWARD);
return UINT2NUM(saved);
}
|
.roundingUp ⇒ Object
Enable the FPU’s upward rounding.
37 38 39 40 41 42 |
# File 'ext/fpu.c', line 37
static VALUE mFPU_rounding_up(VALUE self)
{
int saved = fegetround();
fesetround(FE_UPWARD);
return UINT2NUM(saved);
}
|