double类型精度问题

litsnake 2014-10-29 10:44:47
使用别人接口文件,其中一个参数是金额double类型,

在运行过程老是产生误差,后来debug调试发现问题所在

比如:
double d1 = 1235.2
double d2 = 10.6
double d3 = d1-d2; //本应该d3=1224.6,调试发现d3=1224.5999999999999

然后d3作为接口参数之一送进去,在接口能运作是采用的是截位,直接把结果变成了1224.5,因此老是产生0.1的偏差


如果能让d3保证是1224.6,而不是1224.5999999999999

谢谢
...全文
423 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-10-29
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390676437
#include <stdio.h>
double d1 = 1235.2;
double d2 = 10.6;
double d3;
int main() {
    d3=d1-d2;
    printf("%lf\n",d3);
    printf("%.15lf\n",d3);
    printf("%lg\n",d3);

    printf("\n");

    d3=1224.5999999999999;
    printf("%lf\n",d3);
    printf("%.15lf\n",d3);
    printf("%lg\n",d3);
    return 0;
}
//1224.600000
//1224.600000000000100
//1224.6
//
//1224.600000
//1224.599999999999900
//1224.6
//
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码! 作为一个C程序员,对 scanf,sscanf,fscanf printf,sprintf,fprintf 这类函数的用法,还是要做到“拳不离手,曲不离口”的。 f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. f The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed. g, G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated.
mujiok2003 2014-10-29
  • 打赏
  • 举报
回复
引用 3 楼 litsnake 的回复:
那么 [quote=引用 2 楼 mujiok2003 的回复:] 浮点数都二进制和10进制都有误差的。 考虑用整数吧。
这是三方dll,接收的就是double类型,咋能使用整数?[/quote] 那没有办法, 因为你没有办法精确表示1224.6
litsnake 2014-10-29
  • 打赏
  • 举报
回复
截尾 是第三方dll里面的逻辑,我没有办法改变了, 我送进去的参数值应该1224.6,但是变成了1224.59999999 再经过dll中截尾 ,就变样了,如果dll中四舍五入当然没有问题了, 我原来的方法就是加一个小一点的,传d3+0.05,这样就是1224.64999999,截位之后也是1224.6, 但是这方法感觉有点别扭,看看有没有更好的处理方法
ri_aje 2014-10-29
  • 打赏
  • 举报
回复
金额用整数表达更好,浮点就会带来这种问题。
Saleayas 2014-10-29
  • 打赏
  • 举报
回复
d3=1224.5999999999999 这个值在精度误差允许返回类就是 1224.6,如果你需要有效数字,那么必须使用专门的舍入法。不能简单的截尾。
女神打Boss 2014-10-29
  • 打赏
  • 举报
回复
把dll返回值强转
litsnake 2014-10-29
  • 打赏
  • 举报
回复
那么
引用 2 楼 mujiok2003 的回复:
浮点数都二进制和10进制都有误差的。 考虑用整数吧。
这是三方dll,接收的就是double类型,咋能使用整数?
mujiok2003 2014-10-29
  • 打赏
  • 举报
回复
浮点数都二进制和10进制都有误差的。 考虑用整数吧。
nice_cxf 2014-10-29
  • 打赏
  • 举报
回复
保证不了,在上边截取的时候加一个比较小的数再转就可以了

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧