我试图用python将数字打印成工程格式,但我似乎无法让它工作.语法SEEMS足够简单,但它不起作用.
>>> import decimal
>>> x = decimal.Decimal(1000000)
>>> print x
1000000
>>>> print x.to_eng_string()
1000000
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么会这样.这两个值不相等(一个是字符串,另一个是int).设置各种上下文decimal似乎也没有帮助.任何线索或想法?
尝试使用一些perlin噪声函数时,遇到一个奇怪的错误.突然间,我在所有电话中间都获得了一个off值.将其追溯到fmod()中的不一致返回值.
i=512;
cout << i << "," << fmod(i/102.4,1.f) << "," << fmod(i/102.4,1.f) << endl;
Run Code Online (Sandbox Code Playgroud)
我希望得到以下输出.
512,0,0
Run Code Online (Sandbox Code Playgroud)
但我没有.我明白了
512,0,1
Run Code Online (Sandbox Code Playgroud)
更奇怪的是,如果我多次调用cout线,则错误会在以后的运行中消失.
512,0,1
512,0,0
512,0,0
Run Code Online (Sandbox Code Playgroud)
将值硬编码到函数中(将所有'y'替换为'512')会得到正确的结果(始终返回0).
有没有人见过这样的结果?