关于印刷的问题 [英] Question about printing double

查看:62
本文介绍了关于印刷的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。当double值非常大时,我不能使用

iostream(例如cout)来打印它(但printf效果很好)

(以g ++为单位),为什么?


2.当双倍值非常大时,打印结果

在iostream(例如cout)和printf

之间的差异在vc08中,为什么?


谢谢。

解决方案

Aman JIANG写道:


1.当double值非常大时,我无法使用

iostream(例如cout)来打印它(但是printf效果很好)

以g ++为单位,为什么?



这是什么意思你不能使用std :: cout?编译器是否检测到数字太大并发出错误消息?
做了这个程序

崩溃了吗?输出不是你所期望的? ...


2.当双值非常大时,打印结果

在iostream之间有所不同(例如cout)和printf

在vc08,为什么?



一个错误?


无论如何,发布一些显示问题的代码。也许你的测试代码有未定义的行为。如果没有,您应该联系您的

图书馆的供应商。

Best


Kai-Uwe Bux


9月26日下午3:41,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:


Aman JIANG写道:


1.当double值非常大时,我不能使用

iostream(例如cout)来打印它(但是printf效果很好)
以g ++为单位的
,为什么?



这是什么意思你不能使用std :: cout?编译器是否检测到数字太大并发出错误消息?
做了这个程序

崩溃了吗?输出不是你所期望的? ...



该程序已构建,0错误和0警告,它运行,但



已打印任何消息或值。我的操作系统是XP,你可以测试。


2.当双值非常大时,打印结果

在vost08中iostream(例如cout)和printf

之间有所不同,为什么?



一个错误?


无论如何,发布一些显示问题的代码。也许你的测试代码有未定义的行为。如果没有,您应该联系您的

库的供应商。



我会告诉你一个简单的:


void f(){

double value = 1.23456789e45;

cout<<固定<<值<< endl;

printf("%f\ n",value);

}


On Sep 26,6:29 am,Aman JIANG< AmanJI ... @ gmail.comwrote:


9月26日下午3:41,Kai-Uwe Bux< ; jkherci ... @ gmx.netwrote:


Aman JIANG写道:


1.当a double值非常大,我不能使用

iostream(例如cout)来打印它(但printf效果很好)

以g ++为单位,为什么?


这是什么意思你不能使用std :: cout?编译器是否检测到数字太大并发出错误消息?
做了这个程序

崩溃了吗?输出不是你所期望的? ...



该程序已经构建,0个错误和0个警告,它运行,但



已打印任何消息或值。我的操作系统是XP,你可以测试。


2.当双值非常大时,打印结果

在vost08中iostream(例如cout)和printf

之间有所不同,为什么?


一个错误?


无论如何,发布一些显示问题的代码。也许你的测试代码有未定义的行为。如果没有,您应该联系您的

库的供应商。



我会告诉你一个简单的:


void f(){

double value = 1.23456789e45;

cout<<固定<<值<< endl;

printf("%f\ n",value);


}



FWIW,我没有问题。这是我的代码:


#include< iostream>

#include< iomanip>

#include< ; cstdio>


int main(无效)

{

double value = 1.23456789e45;

std :: cout<< std :: fixed<<值<< std :: endl;

std :: cout<<值<< std :: endl;

printf("%f\ n",value);

返回0;

}

这里是输出:


1234567889999999964160667452302023944549957632.000 000

1234567889999999964160667452302023944549957632.000 000

1234567889999999964160667452302023944549957632.000 000

这是在Mac OS X上使用g ++构建的。


您的代码中必定还有其他内容。


1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?

thanks.

解决方案

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?

What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?

A bug?

Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.
Best

Kai-Uwe Bux


On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?


What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...

The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?


A bug?

Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.

I''ll show you a simple one:

void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);
}


On Sep 26, 6:29 am, Aman JIANG <AmanJI...@gmail.comwrote:

On Sep 26, 3:41 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Aman JIANG wrote:

1. when a double value is very large, I can not use
iostream(e.g. cout) to print it (but printf works well)
in g++, why ?

What does it mean "you cannot use std::cout"? did the compiler detect that
the numbers were too large and issue an error message? did the program
crash? was the output not what you expected? ...


The program has been built, 0 error(s) and 0 warning(s), it runs, but
no
any message or value has been printed. My OS is XP, you can test.

2. when a double value is very large, the printed results
were different between iostream(e.g. cout) and printf
in vc08, why ?

A bug?

Anyway, post some code that exhibits the problem. Maybe your test code has
undefined behavior. If not, your should contact the vendors of your
libraries.


I''ll show you a simple one:

void f() {
double value = 1.23456789e45;
cout << fixed << value << endl;
printf ("%f\n", value);

}

FWIW, I had no problems. Here''s my code:

#include <iostream>
#include <iomanip>
#include <cstdio>

int main(void)
{
double value = 1.23456789e45;
std::cout << std::fixed << value << std::endl;
std::cout << value << std::endl;
printf("%f\n", value);
return 0;
}
And here''s the output:

1234567889999999964160667452302023944549957632.000 000
1234567889999999964160667452302023944549957632.000 000
1234567889999999964160667452302023944549957632.000 000

This was built using g++ on Mac OS X.

There must be something else going on in your code.


这篇关于关于印刷的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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