Proposal: fixing the 'pure' floating point problem.

Philip Miess philip.Miess at yahoo.com
Fri Apr 10 11:06:49 PDT 2009


Denis Koroskin wrote:
> On Sat, 04 Apr 2009 15:19:46 +0400, Philip Miess 
> <philip.Miess at yahoo.com> wrote:
> 
>> of course my example makes no sense
>> try
>>
>> pure float square(float x, invariant roundingMode round = default)
>> {
>>      return x*x;
>> }
>> in case that helps
>>
>> Phil
> 
> I don't see roundingMode used anywhere in your example.
> 

Denis,
	the rounding mode is set globally so if you set it before calling a 
function it would be used to round the results of a floating point 
multiply.
Anyway here is a better example thats much like one I have actually 
compiled.

import std.c.fenv;
import std.math;

pure long myround(real x, int round = fegetround() )
{
	//fsetround(round);
  	return lrint(x);
}

int main(char[][] args)
{
	long result;
	result = round(2.6);
	//result is now 3

	fsetround(FE_DOWNWARD);
	result = round(2.6);
	//result is now 2
}

If DMD was memoizing the function it should not think that these both 
these calls return the same thing.

Unless of course DMD is too smart/not smart enough and doesn't realize 
that the second parameter is important and optimizes it away.

In that case uncomment the call to fesetround with the round parameter 
inside myround(). Also you may want to do things like check if the 
current round mode is the same as the current round mode and only set it 
if it's not. Then you may like to set it back to the original afterwards 
to make it act like a pure function.

Phil



More information about the Digitalmars-d mailing list