我最近遇到了一个问题 可以使用模数除法轻松解决,但输入是浮点数:
给定一个周期函数(例如sin)和一个只能在周期范围内计算它的计算机函数(例如[-π,π]),制作一个可以处理任何输入的函数.
Given a periodic function (e.g. sin) and a computer function that can only compute it within the period range (e.g. [-π,π]),make a function that can handle any input.
明显"的解决方案类似于:
#include <cmath> float sin(float x){ return limited_sin((x + M_PI) % (2 *M_PI) - M_PI); }
为什么这不起作用?我收到此错误:
error: invalid operands of types double and double to binary operator %
有趣的是,它确实适用于 Python:
def sin(x): return limited_sin((x + math.pi) % (2 * math.pi) - math.pi)