O
OliverMarchand
I am suggesting to change the Float.round function to use the
well-known round-to-even method in case of a tie, i.e. x.5, where the
distance to ceil and floor is equal.
round currently uses a round-away-from-zero strategy. This strategy
will introduce a bias when applied to many numbers, i.e. the average of
the rounded numbers will be higher than that of original numbers. This
effect does make a considerable difference, despite the singular nature
of the x.5 values. Consider e.g. the computation of the median of an
array with floats.
Here is a quick hack ruby implementation which adds round to even to
the Float class:
class Float
def signum
if self>=0.0; 1.0
else; -1.0
end
end
def roundte
s = self.signum
f = self*s
rp = f-f.floor
rm = f.ceil-f
if rp>rm
return (s*f.ceil).to_i
elsif rp<rm
return (s*f.floor).to_i
else
if (f.ceil%2)==0; return (s*f.ceil).to_i
else; return (s*f.floor).to_i
end
end
end
end
Round is implemented in float.c. Anyone interested in me implementing
round-to-even there?
tschuess,
Oliver
well-known round-to-even method in case of a tie, i.e. x.5, where the
distance to ceil and floor is equal.
round currently uses a round-away-from-zero strategy. This strategy
will introduce a bias when applied to many numbers, i.e. the average of
the rounded numbers will be higher than that of original numbers. This
effect does make a considerable difference, despite the singular nature
of the x.5 values. Consider e.g. the computation of the median of an
array with floats.
Here is a quick hack ruby implementation which adds round to even to
the Float class:
class Float
def signum
if self>=0.0; 1.0
else; -1.0
end
end
def roundte
s = self.signum
f = self*s
rp = f-f.floor
rm = f.ceil-f
if rp>rm
return (s*f.ceil).to_i
elsif rp<rm
return (s*f.floor).to_i
else
if (f.ceil%2)==0; return (s*f.ceil).to_i
else; return (s*f.floor).to_i
end
end
end
end
Round is implemented in float.c. Anyone interested in me implementing
round-to-even there?
tschuess,
Oliver