Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 14, 2015 The best way to compare floating point values. | ||||
---|---|---|---|---|
| ||||
I wrote this function for comparing two floating point values:
> import std.math;
> import std.traits;
>
> bool isEqual(T)(T v1, T v2) if(isFloatingPoint!T) {
> return T.mant_dig - feqrel(v1, v2) < 2;
> }
What do you think about it?
|
February 14, 2015 Re: The best way to compare floating point values. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Saturday, 14 February 2015 at 09:37:05 UTC, Jack Applegame wrote: > I wrote this function for comparing two floating point values: > >> import std.math; >> import std.traits; >> >> bool isEqual(T)(T v1, T v2) if(isFloatingPoint!T) { >> return T.mant_dig - feqrel(v1, v2) < 2; >> } > > What do you think about it? I wrote a similar function here: https://github.com/nomad-software/dunit/blob/master/source/dunit/toolkit.d#L42 or using an epsilon value: https://github.com/nomad-software/dunit/blob/master/source/dunit/toolkit.d#L134 I don't know if they are useful to you? |
February 14, 2015 Re: The best way to compare floating point values. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Saturday, 14 February 2015 at 10:23:48 UTC, Gary Willoughby wrote:
> I wrote a similar function here:
>
> https://github.com/nomad-software/dunit/blob/master/source/dunit/toolkit.d#L42
>
> or using an epsilon value:
>
> https://github.com/nomad-software/dunit/blob/master/source/dunit/toolkit.d#L134
>
> I don't know if they are useful to you?
Very interesting. Thanks.
|
February 14, 2015 Re: The best way to compare floating point values. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | There is an approxEqual in std.math, in addition in feqrel: http://dlang.org/phobos/std_math.html#.approxEqual It takes maximum absolute and relative difference as arguments. |
Copyright © 1999-2021 by the D Language Foundation