Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Ability to change rounding mode for all floats (cf. #2976) #3149

Merged
merged 11 commits into from
Sep 2, 2013
Prev Previous commit
Next Next commit
Renaming of the rounding modes
  • Loading branch information
andrioni committed Aug 27, 2013
commit 5a064b7778dd488ef76823fe21abffce082a0a3b
4 changes: 2 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export
Reverse,
RevString,
RopeString,
RoundAwayZero,
RoundFromZero,
RoundDown,
RoundingMode,
RoundToNearest,
RoundNearest,
RoundToZero,
RoundUp,
Schur,
Expand Down
14 changes: 7 additions & 7 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MPFR

export
BigFloat,
RoundAwayZero,
RoundFromZero,
get_bigfloat_precision,
set_bigfloat_precision,
with_bigfloat_precision,
Expand All @@ -21,7 +21,7 @@ import
itrunc, eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan,
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
serialize, deserialize, inf, nan, hash,
RoundingMode, RoundDown, RoundingMode, RoundToNearest, RoundToZero,
RoundingMode, RoundDown, RoundingMode, RoundNearest, RoundToZero,
RoundUp

import Base.Math.lgamma_r
Expand All @@ -30,7 +30,7 @@ const ROUNDING_MODE = [0]
const DEFAULT_PRECISION = [256]

# Rounding modes
type RoundAwayZero <: RoundingMode end
type RoundFromZero <: RoundingMode end

# Basic type and initialization definitions

Expand Down Expand Up @@ -598,24 +598,24 @@ end

function get_bigfloat_rounding()
if ROUNDING_MODE[end] == 0
return RoundToNearest
return RoundNearest
elseif ROUNDING_MODE[end] == 1
return RoundToZero
elseif ROUNDING_MODE[end] == 2
return RoundUp
elseif ROUNDING_MODE[end] == 3
return RoundDown
elseif ROUNDING_MODE[end] == 4
return RoundAwayZero
return RoundFromZero
else
error("Invalid rounding mode")
end
end
set_bigfloat_rounding(::Type{RoundToNearest}) = ROUNDING_MODE[end] = 0
set_bigfloat_rounding(::Type{RoundNearest}) = ROUNDING_MODE[end] = 0
set_bigfloat_rounding(::Type{RoundToZero}) = ROUNDING_MODE[end] = 1
set_bigfloat_rounding(::Type{RoundUp}) = ROUNDING_MODE[end] = 2
set_bigfloat_rounding(::Type{RoundDown}) = ROUNDING_MODE[end] = 3
set_bigfloat_rounding(::Type{RoundAwayZero}) = ROUNDING_MODE[end] = 4
set_bigfloat_rounding(::Type{RoundFromZero}) = ROUNDING_MODE[end] = 4

function copysign(x::BigFloat, y::BigFloat)
z = BigFloat()
Expand Down
8 changes: 4 additions & 4 deletions base/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ module Rounding
include("fenv_constants.jl")

export
RoundingMode, RoundToNearest, RoundToZero, RoundUp, RoundDown,
RoundingMode, RoundNearest, RoundToZero, RoundUp, RoundDown,
get_rounding, set_rounding, with_rounding

## rounding modes ##
abstract RoundingMode
type RoundToNearest <: RoundingMode end
type RoundNearest <: RoundingMode end
type RoundToZero <: RoundingMode end
type RoundUp <: RoundingMode end
type RoundDown <: RoundingMode end

set_rounding(::Type{RoundToNearest}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_TONEAREST)
set_rounding(::Type{RoundNearest}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_TONEAREST)
set_rounding(::Type{RoundToZero}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_TOWARDZERO)
set_rounding(::Type{RoundUp}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_UPWARD)
set_rounding(::Type{RoundDown}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_DOWNWARD)

function get_rounding()
r = ccall(:fegetround, Cint, ())
if r == JL_FE_TONEAREST
return RoundToNearest
return RoundNearest
elseif r == JL_FE_DOWNWARD
return RoundDown
elseif r == JL_FE_UPWARD
Expand Down
2 changes: 1 addition & 1 deletion test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ z = BigFloat(30)
# rounding modes
with_bigfloat_precision(4) do
# default mode is round to nearest
down, up = with_bigfloat_rounding(RoundToNearest) do
down, up = with_bigfloat_rounding(RoundNearest) do
BigFloat("0.0938"), BigFloat("0.102")
end
with_bigfloat_rounding(RoundDown) do
Expand Down
8 changes: 4 additions & 4 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ b = 0.5
c = 0x1p-54
d = prevfloat(1.)

# Default rounding direction, RoundToNearest
# Default rounding direction, RoundNearest
@test a + b === 1.
@test - a - b === -1.
@test a - b === -c
Expand All @@ -23,7 +23,7 @@ with_rounding(RoundToZero) do
@test b - a === c
end

# Sanity check to see if we have returned to RoundToNearest
# Sanity check to see if we have returned to RoundNearest
@test a + b === 1.
@test - a - b === -1.
@test a - b == -c
Expand Down Expand Up @@ -52,7 +52,7 @@ b32 = 0.5f0
c32 = (1.f0 - prevfloat(1.f0))/2
d32 = prevfloat(1.0f0)

# Default rounding direction, RoundToNearest
# Default rounding direction, RoundNearest
@test a32 + b32 === 1.0f0
@test - a32 - b32 === -1.0f0
@test a32 - b32 === -c32
Expand All @@ -66,7 +66,7 @@ with_rounding(RoundToZero) do
@test b32 - a32 === c32
end

# Sanity check to see if we have returned to RoundToNearest
# Sanity check to see if we have returned to RoundNearest
@test a32 + b32 === 1.0f0
@test - a32 - b32 === -1.0f0
@test a32 - b32 == -c32
Expand Down