小编Tim*_*Tim的帖子

C中下溢和nan有什么区别?

目前我正在学习浮点异常。我正在写一个带有函数的循环。在该函数中,计算出的值等于0.5。随着循环的进行,输入值除以10

循环:

 for(i = 0; i < e; i++)
 {
     xf /= 10.0;   // force increasingly smaller values for x
     float_testk (xf, i);
 }
Run Code Online (Sandbox Code Playgroud)

功能:

void float_testk(float x, int i)
{
    float result;
   
    feclearexcept(FE_ALL_EXCEPT); // clear all pending exceptions
         
    result = (1 - cosf(x)) / (x * x);
        
    if(fetestexcept(FE_UNDERFLOW) != 0)
        fprintf(stderr,"Underflow occurred in double_testk!\n");
    if(fetestexcept(FE_OVERFLOW) != 0)
        fprintf(stderr,"Overflow occurred in double_testk!\n");
    if(fetestexcept(FE_INVALID) != 0)
        fprintf(stderr,"Invalid exception occurred in double_testk!\n");
   
    printf("Iteration %3d, float result for x=%.8f …
Run Code Online (Sandbox Code Playgroud)

c floating-point computer-science loops sigfpe

5
推荐指数
2
解决办法
203
查看次数

如何以双精度打印?

我对 C 完全陌生,我正在尝试完成一项任务。练习是打印 tan(x),x 从 0 递增到 pi/2。

我们需要在 float 和 double 中打印它。我写了一个似乎有效的程序,但我只打印了浮点数,而我预计会加倍。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    double x;
    
    double pi;
    pi = M_PI;
     
    for (x = 0; x<=pi/2; x+= pi/20)
    {
        printf("x = %lf, tan = %lf\n",x, tan(x));
        
    }
    
    exit(0);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  • 为什么我得到浮点数,而我将变量定义为 double 并在 printf 函数中使用 %lf ?

  • 我需要改变什么才能获得双打作为输出?

c floating-point double

2
推荐指数
1
解决办法
215
查看次数

标签 统计

c ×2

floating-point ×2

computer-science ×1

double ×1

loops ×1

sigfpe ×1