www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Interval Arithmetic

reply Wulfrick <arm.plus gmail.com> writes:
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!
Sep 29 2015
next sibling parent anonymous <a b.cd> writes:
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
Sep 29 2015
prev sibling parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Tue, 29 Sep 2015 21:04:00 +0000
schrieb Wulfrick <arm.plus gmail.com>:

 Is there an interval arithmetic library in D? I couldn=E2=80=99t find one.
=20
 In case I had to write my own, I understand that the IEEE=20
 standard floating point arithmetic provides operations for=20
 rounding up or down certain operations like summing, subtracting,=20
 etc. (thus overriding the default behavior of rounding to nearest=20
 representable).
=20
 How do I access this functionality in D? At first I thought that=20
 std.math.nextDown and nextUp is what I needed, but not so.=20
 Apparently these functions return the previous or next=20
 representable *after* the calculation has been done.
=20
 For example, I would like the value of x+y rounded in the=20
 arithmetic towards -\infty, which may or may not be nextDown(x+y).
=20
 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 =3D 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. --=20 Marco
Oct 01 2015
parent reply ponce <contact gam3sfrommars.fr> writes:
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
Oct 01 2015
parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Thu, 01 Oct 2015 12:03:10 +0000
schrieb ponce <contact gam3sfrommars.fr>:

 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
Oct 01 2015
parent ponce <contact gam3sfrommars.fr> writes:
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.
Oct 02 2015