A clever homebrew modulus implementation

前端 未结 5 1816
甜味超标
甜味超标 2021-02-06 04:29

I\'m programming a PLC with some legacy software (RSLogix 500, don\'t ask) and it does not natively support a modulus operation, but I need one. I do not have access to: modulus

5条回答
  •  抹茶落季
    2021-02-06 04:37

    If you don't mind overly complicating things and wasting computer time you can calculate modulus with periodic trig functions:

    atan(tan(( 12345.678 -5000)*pi/10000))*10000/pi+5000   =   2345.678
    

    Seriously though, subtracting 10000 once or twice (your "implementation 2") is better. The usual algorithms for general floating point modulus require a number of bit-level manipulations that are probably unfeasible for you. See for example http://www.netlib.org/fdlibm/e_fmod.c (The algorithm is simple but the code is complex because of special cases and because it is written for IEEE 754 double precision numbers assuming there is no 64-bit integer type)

提交回复
热议问题