Zsh Mailing List Archive
Messages sorted by:
Re: Floating point modulus
On Jan 11, 4:02pm, Ray Andrews wrote:
} Subject: Re: Floating point modulus
} On 01/11/2015 12:25 PM, Peter Stephenson wrote:
} > I'll leave that to you, but instead of an explicit rounding you could
} > do basically the same calculation but assigned to a variable declared
} > as an integer and output that.xs pws remainder=$1
} Ignore this question if it can't be explained simply, since
} I don't have the knowledge probably to even understand the
} answer, but why is it more complicated
You're conflating two only loosely related things. Above, Peter is
talking about the regression test shell code; you seem to be asking
about the C code change, which already works fine -- except that on
different hardware/OS platforms, different implementations of double-
precision arithmetic may cause the same floating-point operation to
return results that differ in the less-significant decimal places.
Since the tests rely on being able to (textually) compare an actual
result to an expected result, you can't test just any floating point
expression, you have to choose a floating point expression that will
be the same everywhere to however many decimal places.
} /* 25 */ BOOL|OP_A2IO, LR|OP_A2IO, RL|OP_OP, RL|OP_OP, RL|OP_E2,
} -/* 30 */ RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2IO,
} ... are those 'metas'? IOW, is that the internal representation of
} some command syntax? Metafied? Tokenized? ...
There are two tables in math.c, one with symbolic names of all the
operators, and one with symbolic (represented as bitfield) binding and
precedence of those same operators. The tables line up positionally.
Yes, this should probably have been done with structs instead of with
two parallel arrays, but this code was written by a college sophomore
in 1993 or so and barely changed since.
LR and RL are binding, OP_A2 and OP_E2 are assignment or expression
and the number of operands, and a suffix of IO means the operator is
"integer only". This information is used by the math parser. So my
patch removed the "integer only" bit from the map for % and %= and
changed the actual implemention to call fmod() for non-integers. The
change is very small compared to the diff context produced.
Barton E. Schaefer
Messages sorted by: