basvandijk / scientific

Arbitrary-precision floating-point numbers represented using scientific notation
BSD 3-Clause "New" or "Revised" License
73 stars 40 forks source link

Add conversion function which cares about bounds #16

Closed fujimura closed 10 years ago

fujimura commented 10 years ago

I found there seems to be no handy way to find that a scientific is out of bounds of some bounded number types, like Int64 or Double. So I'd like to propose new conversion functions which cares about these bounds. It will be useful if the somebody wants to treat out of bound values as an exception, not zero or infinity.

basvandijk commented 10 years ago

Thanks I think these are useful!

I corrected a little bug in your implementation. The condition: min <= s || s <= max as the wrong way around. I extended your test to check for this case.

I also added protection against untrusted Scientifics that could contain huge exponents like 1e1000000000 which when given to the function would create an Integer that would fill up all space and crash your program.

I also renamed toInt to toBoundedInteger which I think is a more descriptive.

fujimura commented 10 years ago

Thanks! Sorry for making trouble by my bugs.

basvandijk commented 10 years ago

Sorry for making trouble by my bugs.

No problem at all.

Thanks for the contribution. Speaking of which, I should add a CONTRIBUTORS file.

basvandijk commented 10 years ago

I renamed toRealFloat' to toBoundedRealFloat. Also not a great name but the ' convention is usually reserved for strict variants of functions.

I released this as 0.3.3.0.

fujimura commented 10 years ago

Thanks!