- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
floats, a plague on us all
Mon, 2009-11-09, 20:12
Ponder the implications:
scala> Iterator.iterate(Int.MaxValue)(_ - 1) takeWhile (_ == Int.MaxValue.toFloat) length
res0: Int = 64
Even if we jump down by factor of 100:
scala> Iterator.iterate(num)(_ - 1) takeWhile (_ == num.toFloat) length
res2: Int = 2
Float's promiscuity is a real fly in the consistency ointment. I think
the best and probably only livable way out of it is to punt on Floats
and Doubles: pick some comparatively sensible bundle of contradictions
and emblazon them on their sandwich boards.
I suspect most people (given an improbably long time to think things
over) would rather have fully internally and externally consistent
behavior among byte/short/int/long/char and boot the fractionals, than
to in any way compromise the consistency of the first five for the last
two. Since you can't sensibly compare them for equality anyway...
Mon, 2009-11-09, 20:47
#2
Re: floats, a plague on us all
On Mon, Nov 09, 2009 at 02:17:27PM -0500, Matt Fowles wrote:
> It would be nice if 0.0f and 0.0 compare identically with 0 and 0L.
> But other than that, most people know that testings for floating point
> equality without a tolerance is just silly.
Thinking on it, we can salvage almost everything by stringing up some
razor wire. Double has no issue, it's big enough to hold all the Ints.
Even BigInt and BigDecimal are no problem should we choose to include
them in the mix.
So we could spec Float to do the right thing for whole values from
Short.MinValue to Short.MaxValue (could be larger but it's the obvious
cutoff) and then people can still do all the foolish things with Floats
they shouldn't be doing anyway.
Paul~
It would be nice if 0.0f and 0.0 compare identically with 0 and 0L.
But other than that, most people know that testings for floating point
equality without a tolerance is just silly.
Matt
On Mon, Nov 9, 2009 at 2:12 PM, Paul Phillips wrote:
> Ponder the implications:
>
> scala> Iterator.iterate(Int.MaxValue)(_ - 1) takeWhile (_ == Int.MaxValue.toFloat) length
> res0: Int = 64
>
> Even if we jump down by factor of 100:
>
> scala> Iterator.iterate(num)(_ - 1) takeWhile (_ == num.toFloat) length
> res2: Int = 2
>
> Float's promiscuity is a real fly in the consistency ointment. I think
> the best and probably only livable way out of it is to punt on Floats
> and Doubles: pick some comparatively sensible bundle of contradictions
> and emblazon them on their sandwich boards.
>
> I suspect most people (given an improbably long time to think things
> over) would rather have fully internally and externally consistent
> behavior among byte/short/int/long/char and boot the fractionals, than
> to in any way compromise the consistency of the first five for the last
> two. Since you can't sensibly compare them for equality anyway...
>
> --
> Paul Phillips | A national political campaign is better than the
> Moral Alien | best circus ever heard of, with a mass baptism and
> Empiricist | a couple of hangings thrown in.
> ha! spill, pupil | -- H. L. Mencken
>