Floating Point
Floating Point Intermediate Values
On many computers, greater precision operations do not take any longer than lesser precision operations, so it makes numerical sense to use the greatest precision available for internal temporaries. The philosophy is not to dumb down the language to the lowest common hardware denominator, but to enable the exploitation of the best capabilities of target hardware.For floating point operations and expression intermediate values, a greater precision can be used than the type of the expression. Only the minimum precision is set by the types of the operands, not the maximum. Implementation Note: On Intel x86 machines, for example, it is expected (but not required) that the intermediate calculations be done to the full 80 bits of precision implemented by the hardware.
It's possible that, due to greater use of temporaries and common subexpressions, optimized code may produce a more accurate answer than unoptimized code.
Algorithms should be written to work based on the minimum precision of the calculation. They should not degrade or fail if the actual precision is greater. Float or double types, as opposed to the extended type, should only be used for:
- reducing memory consumption for large arrays
- data and function argument compatibility with C
Complex and Imaginary types
In existing languages, there is an astonishing amount of effort expended in trying to jam a complex type onto existing type definition facilities: templates, structs, operator overloading, etc., and it all usually ultimately fails. It fails because the semantics of complex operations can be subtle, and it fails because the compiler doesn't know what the programmer is trying to do, and so cannot optimize the semantic implementation.
This is all done to avoid adding a new type. Adding a new type means that the compiler
can make all the semantics of complex work "right". The programmer then can rely on a
correct (or at least fixable
Coming with the baggage of a complex type is the need for an imaginary type. An
imaginary type eliminates some subtle semantic issues, and improves performance by not
having to perform extra operations on the implied 0 real part.
Imaginary literals have an i suffix:
ireal j = 1.3i;
There is no particular complex literal syntax, just add a real and imaginary type:
cdouble cd = 3.6 + 4i;
creal c = 4.5 + 2i;
Complex numbers have two properties:
.re get real part
.im get imaginary part as a real
For example:
cd.re is 4.5 double
cd.im is 2 double
c.re is 4.5 real
c.im is 2 real
Rounding Control
IEEE 754 floating point arithmetic includes the ability to set 4 different rounding modes.
These are accessible via the functions in std.c.fenv.
Exception Flags
IEEE 754 floating point arithmetic can set several flags based on what happened with a
computation: [blah, blah, blah].
These flags can be set/reset via the functions in std.c.fenv.
Floating Point Comparisons
In addition to the usual < <= > >= == != comparison
operators, D adds more that are
specific to floating point. These are
!<>=
<>
<>=
!<=
!<
!>=
!>
!<>
and match the semantics for the
NCEG extensions to C.
Floating point comparison operators
Operator Relations Invalid? Description
> < = ?
< F T F F yes less than
> T F F F yes greater than
<= F T T F yes less than or equal to
>= T F T F yes greater than or equal to
== F F T F no equal to
!= T T F T no unordered, less than, or greater than
!<>= F F F T no unordered
<> T T F F yes less than or greater than
<>= T T T F yes less than, equal to, or greater than
!<= T F F T no unordered or greater than
!< T F T T no unordered, greater than, or equal to
!>= F T F T no unordered or less than
!> F T T T no unordered, less than, or equal to
!<> F F T T no unordered or equal to