简体   繁体   English

C ++中的浮点数学运算输出

[英]floating-point math operations output in C++

I am trying to right a simple math program, and I am facing difficulty getting correct formatted output. 我正在尝试纠正一个简单的数学程序,并且遇到获取正确格式输出的困难。

Here is a sample code: 这是一个示例代码:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

const double PI = 3.14159265358979;

int main()
{
        double a,b,c,d;

        a = cos(30*PI/180);
        b = sin(30*PI/180);
        c = cos(90*PI/180);
        d = sin(90*PI/180);

        cout << a << endl;
        cout << b << endl;
        cout << c << endl;
        cout << d << endl;
}

which give the following output: 给出以下输出:

0.866025 0.866025
0.5 0.5
1.61554e-015 1.61554e-015
1 1

I tried to use round as follows but round will round all of them 我尝试如下使用舍入法,但是舍入法将全部舍入

cout << round(a) << endl;
cout << round(b) << endl;
cout << round(c) << endl;
cout << round(d) << endl;  

which gives the following output: 提供以下输出:

1 1
0 0
0 0
1 1

at last I tried fixed but it is fixed for all 最后,我尝试了固定,但它对所有人都是固定的

cout << fixed << a << endl;
cout << fixed << b << endl;
cout << fixed << c << endl;
cout << fixed << d << endl;

Output 产量

0.866025 0.866025
0.500000 0.500000
0.000000 0.000000
1.000000 1.000000

What I am trying to get is output output like: 我想要得到的是输出输出,如:

0.866025 0.866025
0.5 0.5
0 0
1 1

I know floating-point is hard to deal with due to representing the unlimited by a limited storage. 我知道浮点数很难处理,因为有限的存储代表了无限的存储空间。

I went through a lot of readings about floating-point numbers but have not find how can I get the required result using only the standard C++ libraries. 我阅读了很多有关浮点数的文章,但没有找到如何仅使用标准C ++库获得所需结果的方法。

and because angles 30 and 90 is just a sample I cannot use different output techniques for each variable. 由于角度30和90只是一个示例,因此我不能对每个变量使用不同的输出技术。

I would prefer sticking to cout rather than printf as long as possible. 我宁愿尽可能地坚持cout而不是printf。

I appreciate your help in advance. 非常感谢您的帮助。

I assume the offending output is the one for c using scientific notation. 我认为使用科学计数法的冒犯性输出是c输出。 If so, you'll need to deal with the values separately: the flexibly format (neither std::ios_base::fixed nor std::ios_base::scientific is set and the format is equivalent to the use of printf() 's %g format) will print values in a fashion which represents values with the set precision as best as it can. 如果是这样,则需要分别处理这些值:灵活的格式(既未设置std::ios_base::fixed也未设置std::ios_base::scientific ,并且该格式等效于使用printf()%g格式)将以最能代表设置的精度的方式打印值。 This approach will use scientific notation when the number of digits needed to reasonably represent the value. 当合理表示值所需的位数时,此方法将使用科学计数法。

One way to deal with this situation is to explicitly detect "bad" values, eg, values below some threshold. 解决这种情况的一种方法是显式检测“不良”值,例如低于某个阈值的值。 If you want to avoid checking each output individually you can imbue() a custom std::locale object with a custom std::num_put<...> facet, eg: 如果要避免单独检查每个输出,则可以使用自定义std::num_put<...>方面,将自定义std::locale对象std::num_put<...> imbue() std::num_put<...> imbue() ,例如:

#include <iostream>
#include <locale>
#include <iomanip>
#include <cmath>

const double PI = 3.14159265358979;

struct num_put: std::num_put<char> {
    iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill, double v) const {
        if (v < 1e-4) {
            *to = '0';
            ++to;
            return to;
        }
        return std::num_put<char>::do_put(to, fmt, fill, v);
    }
};

int main()
{
    std::cout.imbue(std::locale(std::locale(), new num_put));

        double a,b,c,d;

        a = std::cos(30*PI/180);
        b = std::sin(30*PI/180);
        c = std::cos(90*PI/180);
        d = std::sin(90*PI/180);

        std::cout << a << '\n';
        std::cout << b << '\n';
        std::cout << c << '\n';
        std::cout << d << '\n';
}

This quick demo also fixed a few unrelated problems: 此快速演示还修复了一些不相关的问题:

  • do not use std:endl : if you mean to flush the stream explicitly use std::flush 不要使用std:endl :如果要显式刷新流,请使用std::flush
  • using directives are bad using指令是不好的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM