[BUG] expr doesn't like floats with modulo

Oct 12, 2008 at 4:58pm

[BUG] expr doesn't like floats with modulo

Hello.

I just realized that [expr] doesn’t deal with float arguments and %.
For example, [expr $f1%1.2] will output the same as [expr $f1%1].
I would call this a bug because [%] deals with float arguments but maybe
it’s an expected result ?
Example patch below.

Ciao

– Pasted Max Patch, click to expand. –
#40299
Oct 12, 2008 at 10:09pm

The operator % in C only applies to integers, although of course the idea of a “remainder” is apparently simple to apply to floats.

Presumably the % object implements a workaround to simulate a % operation on floats (if it existed in C).

[ expr (($f1 / $f2) - int($f1 / $f2)) * $f2 ]

works in expr, for example.

Beware, though using this expression or the % object due to the floating point representation accuracy:

Output from % object:

1.2 % 1.2 = 0.0
2.4 % 1.2 = 0.0
3.6 % 1.2 = 1.2
4.8 % 1.2 = 0.0
6.0 % 1.2 = 1.2

You’d expect the results to be zero.

#142422
Oct 13, 2008 at 7:11am

Thanx for the explanations.
I thought that the same kind of “workaround” was used in [expr] though.

Ciao

#142423

You must be logged in to reply to this topic.