Re: Interval Arithmetic
On Thursday, 1 October 2015 at 21:13:30 UTC, Marco Leise wrote: Nice to have in Phobos. I assume you have to set the correct control word depending on whether you perform math on the FPU or via SSE (as is standard for x86_64)? And I assume further that DMD always uses FPU math and other compilers provide flags to switch between FPU and SSE? I don't know which compiler use which. On x86_64, a compiler is in practice free to mix-and-match FPU and SSE, the instructions are still there and working.
Re: Interval Arithmetic
Am Tue, 29 Sep 2015 21:04:00 + schrieb Wulfrick <arm.p...@gmail.com>: > Is there an interval arithmetic library in D? I couldn’t find one. > > In case I had to write my own, I understand that the IEEE > standard floating point arithmetic provides operations for > rounding up or down certain operations like summing, subtracting, > etc. (thus overriding the default behavior of rounding to nearest > representable). > > How do I access this functionality in D? At first I thought that > std.math.nextDown and nextUp is what I needed, but not so. > Apparently these functions return the previous or next > representable *after* the calculation has been done. > > For example, I would like the value of x+y rounded in the > arithmetic towards -\infty, which may or may not be nextDown(x+y). > > Any luck? > Thanks for reading! Yes, Phobos provides you with this thing: http://dlang.org/phobos/std_math.html#.FloatingPointControl Read the help carefully. End of the scope generally means "}". You can also use the C standard library from D and use: http://www.cplusplus.com/reference/cfenv/fesetround/ import core.stdc.fenv; fesetround( FE_DOWNWARD ); auto z = x + y; And if all that still isn't enough you can write it in inline assembler using the `fldcw` mnemonic. Note that the FP control word is per thread and any external code you call or even buggy interrupt handlers could change or reset it to defaults. Known cases include a faulty printer driver and Delphi's runtime, which enables FP exceptions to throw exceptions on division by 0. Just saying this so if it ever happens you have it in the back of your mind. Against interrupt handlers you probably cannot protect, but when calling other people's code it would be best not depend on what the FP control word is set to on return. `FloatingPointControl` is nice here, because you can temporarily set the rounding mode directly for a block of FP instructions where no external libraries are involved. -- Marco
Re: Interval Arithmetic
On Thursday, 1 October 2015 at 11:40:28 UTC, Marco Leise wrote: Note that the FP control word is per thread and any external code you call or even buggy interrupt handlers could change or reset it to defaults. Known cases include a faulty printer driver and Delphi's runtime, which enables FP exceptions to throw exceptions on division by 0. Just saying this so if it ever happens you have it in the back of your mind. Against interrupt handlers you probably cannot protect, but when calling other people's code it would be best not depend on what the FP control word is set to on return. `FloatingPointControl` is nice here, because you can temporarily set the rounding mode directly for a block of FP instructions where no external libraries are involved. I have a RAII struct to save/restore the FP control word. It also handle the SSE control word which unfortunately exist. https://github.com/p0nce/dplug/blob/master/plugin/dplug/plugin/fpcontrol.d
Re: Interval Arithmetic
Am Thu, 01 Oct 2015 12:03:10 + schrieb ponce: > I have a RAII struct to save/restore the FP control word. > It also handle the SSE control word which unfortunately exist. > > https://github.com/p0nce/dplug/blob/master/plugin/dplug/plugin/fpcontrol.d Nice to have in Phobos. I assume you have to set the correct control word depending on whether you perform math on the FPU or via SSE (as is standard for x86_64)? And I assume further that DMD always uses FPU math and other compilers provide flags to switch between FPU and SSE? -- Marco
Re: Interval Arithmetic
On Tuesday, 29 September 2015 at 21:04:06 UTC, Wulfrick wrote: Is there an interval arithmetic library in D? I couldn’t find one. None I am aware of. In case I had to write my own, I understand that the IEEE standard floating point arithmetic provides operations for rounding up or down certain operations like summing, subtracting, etc. (thus overriding the default behavior of rounding to nearest representable). How do I access this functionality in D? At first I thought that std.math.nextDown and nextUp is what I needed, but not so. Apparently these functions return the previous or next representable *after* the calculation has been done. For example, I would like the value of x+y rounded in the arithmetic towards -\infty, which may or may not be nextDown(x+y). Any luck? Thanks for reading! fencv.h [1] + a few extern(C) declarations could work - changes the rounding mode. Maybe there is an inline ASM solution, too. I have never tried to use that from D. The FENV_ACCESS pragma could cause problems - don't know how to pass that info to a D compiler (never tried to figure it out). It may be easier to generate an binding for an existing C/C++ lib, e.g. [2] (page is in German, but the downloadable tar.gz ("komprimierte (gzipped) tar-Datei") contains an English readme. Boost also contains an interval arithmetic lib [3], but the use of C++ templates will most likely force you to write some glue code in C++... [1] http://www.cplusplus.com/reference/cfenv/ [2] http://www.ti3.tuhh.de/keil/profil/ (GPL) [3] http://www.boost.org/doc/libs/1_58_0/libs/numeric/interval/doc/interval.htm
Interval Arithmetic
Is there an interval arithmetic library in D? I couldn’t find one. In case I had to write my own, I understand that the IEEE standard floating point arithmetic provides operations for rounding up or down certain operations like summing, subtracting, etc. (thus overriding the default behavior of rounding to nearest representable). How do I access this functionality in D? At first I thought that std.math.nextDown and nextUp is what I needed, but not so. Apparently these functions return the previous or next representable *after* the calculation has been done. For example, I would like the value of x+y rounded in the arithmetic towards -\infty, which may or may not be nextDown(x+y). Any luck? Thanks for reading!