Need to track floating-point error: seeking libraries for interval arithmetic, affine arithmetic, stochastic arithmetic, etc.

Joe1000

New member
Joined
Nov 22, 2022
Messages
3
Programming Experience
10+
I'm looking for libraries with which I can track floating-point error. That is, evaluate floating-point expressions, and get back not just their value, but also an indication of its error.

My reading shows that this is generically called self-validation ( see intro to https://tema.sbmac.org.br/tema/article/download/352/291 ) and that techniques include interval arithmetic ( JuliaIntervals ), affine arithmetic ( GitHub - JuliaIntervals/AffineArithmetic.jl: Affine arithmetic in Julia ), and stochastic arithmetic ( GitHub - ffevotte/StochasticArithmetic.jl: Stochastic Arithmetic to diagnose Floating-Point problems in Julia ). Using these, the programmer calculates with types that look as though they're floats — you can use operators + - * / ^ as usual — but that evaluate to a pair containing both the value and the error.

Those three links are to Julia code write-ups, which demonstrate the ideas nicely. Sadly, I've found almost nothing for C#, and I now wonder: do C# developers care at all about how accurate their results are? Shouldn't Microsoft publish such libraries? After all, they presumably need them to validate their own code. One library I did find was Marcel Neumann’s IntSharp at GitHub - selmaohneh/IntSharp: A rigorous interval arithmetic library for .NET , but I don't know whether he's still maintaining it.

Any info would be very welcome.
 
Microsoft recommends using the decimal class when needing to avoid floating point errors.
 
Microsoft recommends using the decimal class when needing to avoid floating point errors.
That isn't what I asked, though. There may be reasons why I can't use decimal, have to stick with floating-point, and need to find out how much error it's caused.

But as you know about decimal, could you tell me something? If Microsoft is promoting it for error-free arithmetic, do they also provide versions of exp(), sin() etc. that work in it? I need these functions, so what should I do? Eric Lippert in a StackOverflow answer at Math operations using System.Decimal in C# says that
decimal and double are both inaccurate. The representation error of decimals is zero when the quantity being represented is exactly equal to a fraction of the form (x/10n) for suitable choices of x and n. The representation error of doubles is zero when the quantity is exactly equal to a fraction of the form (x/2n) again for suitable choices of x and n.

If the quantities you are dealing with are not fractions of that form then you will get some representation error, period. In particular, you mention taking square roots. Many square roots are irrational numbers; they have no fractional form, so any representation format that uses fractions is going to give small errors.
 
Last edited:
Looks like you should put one of those other libraries which track floating point errors.

As I understand things, most general purpose programming languages do not track floating point errors. Most of them just follow the IEEE standards for floating point, and they do that by offloading the floating point work to the processor, as well as use battle tested algorithms for trigonometric and other functions. I don't know what the CLR team does for testing these trigonometric and other functions.
 
Unfortunately, the best libraries for tracking floating-point error are not in C#. Although I linked to one that was, it's limited compared to those that aren't. The best are in Julia. Does anyone know, is it possible to ☆easily☆ call Julia from C#? Preferably automatically, so that I can just give the Julia source and the names of the functions I want to call, and something generates the inter-language calls from that.
 
Gotta love autocorrect. I meant to say "port", not "put" when I said "Look like you should put one of those libraries which track floating point errors."
 
If you want to call your Julia library, you'll need to make sure the functions are marked up with @cfunction:

Once the markup is done, you'll need to script something to generate the appropriate P/Invoke signatures on the C# side. As far as I know, there isn't currently a tool that just grabs all the C exports of any C library and automatically generates P/Invokes. At some point in the past, there was something that did that for COM/OLE type libraries, but I don't know about straight C libraries.
 
Back
Top Bottom