MSVC 2013和2015:printf中的浮点舍入() [英] MSVC 2013 and 2015: floating point rounding in printf()

查看:84
本文介绍了MSVC 2013和2015:printf中的浮点舍入()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSVC 2013中编译此代码时,您将获得1.58:


printf("%0.2f",1.575) ); b
$
如果使用MSVC 2015编译它会打印1.57。

它们可能改变了printf()使用的舍入方法?

如何将printf()的行为切换为以前的行为?

如何在printf()中设置舍入方法?

解决方案


在MSVC 2013中编译此代码时,您将获得1.58:


printf("%0.2f",1.575);


但如果使用MSVC 2015进行编译,它会打印1.57。

他们可能改变了printf()使用的舍入方法?

如何将printf()的行为转换为以前的行为?

如何在printf()中设置舍入方法?


1.575的内部二进制表示约等于


1.5749999999999999555911 ....


所以正确答案是1.57


最新版本的VC,GCC和CLANG都给出了这个答案。


When you compile this code in MSVC 2013, you'll get 1.58:

printf ("%0.2f", 1.575);

But it prints 1.57 if compiled using MSVC 2015.
They probably changed rounding method which printf() uses?
How to switch behaviour of printf() to what was before?
How to set rounding method in printf()?

解决方案

When you compile this code in MSVC 2013, you'll get 1.58:

printf ("%0.2f", 1.575);

But it prints 1.57 if compiled using MSVC 2015.
They probably changed rounding method which printf() uses?
How to switch behaviour of printf() to what was before?
How to set rounding method in printf()?

The internal binary representation of 1.575 is approximately equal to

1.5749999999999999555911....

so the correct answer is 1.57

The latest versions of VC, GCC and CLANG all give this answer.


这篇关于MSVC 2013和2015:printf中的浮点舍入()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆