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 delayedFloatingOrInteger #86

Closed goldfirere closed 2 years ago

goldfirere commented 2 years ago

I'm writing a rather dynamically typed application where I need to convert a Scientific, but I don't know the types to convert to until after I know whether the Scientific is floating or not. But the current interface to floatingOrInteger requires preselection of the types.

Happily with -XImpredicativeTypes, we can make a more general interface, though it has downsides, as documented in this patch.

I'd understand if this is too, say, avant garde for this library, but I thought I'd give a shot at suggesting this.

phadej commented 2 years ago

Do you get any power more over converting to Either Integer Double? and then using fromInteger or realToFrac.

In any case, conditional APIs are out of question.

goldfirere commented 2 years ago

Do you get any power more over converting to Either Integer Double? and then using fromInteger or realToFrac.

Potentially, I imagine, if, say, the floating-point representation had more precision than Double, or the integral representation were optimized to store numbers in the range expected in a particular application. My particular needs don't imagine such exotic scenarios, but I'm building a library whose users may indeed have them.

I feel confident that my suggested function is useful in scenarios that the current API does not serve. Whether these scenarios arrive often enough -- or whether the benefit is large enough -- to be worth the cost of maintaining and documenting the function (and potentially confusing users about which variant to choose), I don't know.

In any case, conditional APIs are out of question.

Fair enough. I would likely agree in your shoes. Thanks for entertaining the thought!