scanf不占用长双 [英] scanf not taking in long double

查看:160
本文介绍了scanf不占用长双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



(请原谅我可怜的英语)

  #include< iostream> 
#include< cstdlib>
#include< math.h>
using namespace std;
int main()
{
int n;
scanf(%d,& n);
long double a,b,c,ha,hb,hc,ma,cosa,r,l,res,area;
for(int i = 0; i {
scanf(%Lf%Lf%Lf%Lf,& a,& ha,& hb ,& hc); //这是问题所在,
//我需要读取4 long double a,ha,hb,hc
printf(%Lf%Lf%Lf%Lf \ n,a,ha,hb,hc); //但它返回错误的答案所以
//我使用printf检查,ps:代码工作与float但不是双或
// long double
ha * = 3; hb * = 3; hc * = 3;
c =(a * ha)/ hc; b =(a * ha)/ hb;
ma = sqrt(0.5 * b * b + 0.5 * c * c-0.25 * a * a);
cosa = ha / ma;
r =(2 * ma)/ 3;
l = b *(b-sqrt(a * a-hb * hb))/ ha;
res = sqrt(l * l + r * r-2 * cosa * l * r);
area = a * ha / 2;
printf(%。3Lf%.3Lf \\\
,area,res);
}
system(PAUSE);
return 0;}
}

输入:

  2 
3.0 0.8660254038 0.8660254038 0.8660254038
657.8256599140 151.6154399062 213.5392629932 139.4878846649

,并在这里有什么节目,在命令行:

  2 
3.0 0.8660254038 0.8660254038 0.8660254038
3.000000 -4824911833695204400000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000.000000 284622047019579100000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000.0
00000 0.866025
-2.000 0.000
657.8256599140 151.6154399062 213.5392629932 139.4878846649
657.825660 -0.000000 28969688850499604000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000.000000 213.539263
-2.000 0.000
按任意键继续。 。 。

我想知道为什么scanf函数不会在长双取



DIV CLASS =h2_lin>解决方案

开发 - C ++使用MinGW的,它使用gcc编译器和Microsoft运行时库。不幸的是,这些组件的基础类型不同意用于长双(64与80或96位,我认为)。 Windows假设 long double 的大小与 double 相同; gcc使 long double 更大。



这两种选择都是有效的,但是组合会导致C和C ++ 。



如果你不需要额外的范围和精度,你可以读入一个 double long double



否则,您可以写入或借用一个自定义字符串到 long双转换器,或只使用不同的实现。



EDIT



更多详细信息:



Microsoft自己的编译器和运行时库在将 long double 位,大小与 double 相同。语言标准允许这个(它需要 long double 至少 double ,但对两者都有相同的要求),但它似乎很奇怪,它没有利用x86的80位浮点硬件。



gcc on x86将 long double 作为96位( sizeof(long double)== 12 )。我认为只有80位是重要的;额外的16位用于对齐目的。



MinGW使用gcc作为其编译器,但使用Microsoft的运行时库。对于大多数语言特性,这个工作正常,但是 long double 的不匹配意味着你可以使用long double做计算,但是你不能传递long double值(或指向它们的指针)到运行时库。这是MinGW中的一个错误。



MinGW中有解决方法。您可以通过在gcc命令行中传递 -D__USE_MINGW_ANSI_STDIO 或通过添加一行来定义 __ USE_MINGW_ANSI_STDIO p>

  #define __USE_MINGW_ANSI_STDIO 


$ b b

到您的源文件。 (它必须在 #include< stdio.h> 之前定义。)注释者paulsm4说, -ansi -posix 选项导致MinGW使用自己的符合库(我没有理由怀疑这一点,但我目前不能够确认)。或者你可以直接调用 __ mingw_printf()



假设你在Windows上,Cygwin可能是一个很好的选择(它使用gcc,但它不使用Microsoft的运行时库)。或者您可以在内部使用 long double ,但对于I / O则可以使用 double


i have problem with scanf not reading long double in the code below:

(please excuse my poor English)

#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main()
    {
    int n;
    scanf("%d",&n);
    long double a,b,c,ha,hb,hc,ma,cosa,r,l,res,area;
    for (int i=0;i<n;i++)
    {
        scanf("%Lf %Lf %Lf %Lf",&a,&ha,&hb,&hc);//this is where the problem lies,
  //i need to read 4 long double a,ha,hb,hc
        printf("%Lf %Lf %Lf %Lf\n",a,ha,hb,hc);//but it returned wrong answer so
  //i used printf to check, ps: the code works with float but not with double or
  //long double
        ha*=3;hb*=3;hc*=3;
        c=(a*ha)/hc; b=(a*ha)/hb;
        ma=sqrt(0.5*b*b+0.5*c*c-0.25*a*a);
        cosa=ha/ma;
        r=(2*ma)/3;
        l=b*(b-sqrt(a*a-hb*hb))/ha;
        res=sqrt(l*l+r*r-2*cosa*l*r);
        area=a*ha/2;
        printf("%.3Lf %.3Lf\n",area,res);
    }
    system("PAUSE");
    return 0;}
}

here's the input:

2
3.0 0.8660254038 0.8660254038 0.8660254038
657.8256599140 151.6154399062 213.5392629932 139.4878846649

and here what's show in the command line:

2
3.0 0.8660254038 0.8660254038 0.8660254038
3.000000 -4824911833695204400000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000.000000 284622047019579100000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000.0
00000 0.866025
-2.000 0.000
657.8256599140 151.6154399062 213.5392629932 139.4878846649
657.825660 -0.000000 28969688850499604000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000.000000 213.539263
-2.000 0.000
Press any key to continue . . .

I want to know why scanf won't take in long double in the code and how to fix it.

Thanks in advance!

解决方案

Dev-c++ uses MinGW, which uses the gcc compiler and the Microsoft runtime library. Unfortunately, those components disagree on the underlying type to be used for long double (64 vs. 80 or 96 bits, I think). Windows assumes long double is the same size as double; gcc makes long double bigger.

Either choice is valid, but the combination results in a broken C and C++ implementation.

If you don't need the extra range and precision, you can read into a double and store into a long double.

Otherwise, you can write or borrow a custom string to long double converter, or just use a different implementation.

EDIT

More details:

Microsoft's own compiler and runtime library are consistent in treating long double as 64 bits, the same size as double. The language standard permits this (it requires long double to be at least as wide as double, but places the same requirements on both), but it does seem odd that it doesn't take advantage of the x86's 80-bit floating-point hardware.

gcc on x86 treats long double as 96 bits (sizeof (long double) == 12). I think only 80 of those bits are significant; the extra 16 bits are for alignment purposes.

MinGW uses gcc as its compiler, but uses Microsoft's runtime library. For most language features, this works fine, but the mismatch for long double means that you can do computations with long double, but you can't pass long double values (or pointers to them) to the runtime library. It's a bug in MinGW.

There are workarounds within MinGW. You can define the macro __USE_MINGW_ANSI_STDIO, either by passing -D__USE_MINGW_ANSI_STDIO on the gcc command line or by adding a line

#define __USE_MINGW_ANSI_STDIO

to your source files. (It has to be defined before #include <stdio.h>.) A commenter, paulsm4, says that the -ansi and -posix options cause MinGW to use its own conforming library (I have no reason to doubt this, but I'm not currently able to confirm it). Or you can call __mingw_printf() directly.

Assuming you're on Windows, Cygwin might be a good alternative (it uses gcc, but it doesn't use Microsoft's runtime library). Or you can use long double internally, but double for I/O.

这篇关于scanf不占用长双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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