怎么做圆号。电力问题

时间:2016-10-11 07:54:55

标签: c++ math

我有2个数学参数:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
    float a, b, x, f;
    printf("\nx=");
    scanf("%f", &x);
    a = 1/(12*pow(x,2) + 7*x-5);
    printf("\na=%.3f", a);
    b = pow(x,2)*pow(x,3);
    b=fabs(b);
    printf("\nb=%.3f", b);
    if (a + b <= 10)    
        if (2*pow(b,2) > 0 && 4*pow(a,2) > 0.00001) {
            f = 4*pow(a,2)-2*pow(b,4);
            printf("\nf1=%.3f", f);
        }
        else  {
            printf("\ x is worng (f1)");
            return 2;
        }
    else
        if (b!=0) {
            f = a/b;
            printf("\nf2=%.3", f);
        }
        else  {
            printf("\ x is wrong (f2)");
            return 3;
        }
    return 0;
}

有时“f”的结果小于0.001。我必须使用printf("\nf2=%.3", f);,以便在点后只显示3个数字。我想在不改变结果的情况下看到它。在我看来,我想到将它乘以10 ^( - n)。但我想不出办法。如果有其他想法会很棒。

2 个答案:

答案 0 :(得分:4)

std::scientific就是为了这个:

#include <iostream>
#include <iomanip>
std::cout << std::setprecision(3) << std::scientific << f;

答案 1 :(得分:-1)

你的倒数第二个版本被破坏了:

printf("\nf2=%.3", f);

test.c:3:20: warning: conversion lacks type at end of format [-Wformat=]
             printf("\nf2=%.3", f);
                    ^
test.c:3:20: warning: too many arguments for format [-Wformat-extra-args]

关于使用printf的一个例子:

printf("%.3f", 1.121212); // prints 1.121

要在printf中使用科学记数法,请使用%e

printf("%.3e", 0.000125); // prints 1.250e-04