如何做整数.权力的问题

How to make round number. Power issues

本文关键字:问题 何做 整数      更新时间:2023-10-16

我有两个数学参数:

#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)但我想不出办法。如果有其他的主意就太好了。

std::scientific就是为了这个:

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

你倒数第二个打印坏了:

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