如何做整数.权力的问题
How to make round number. Power issues
我有两个数学参数:
#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
相关文章:
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 做对了一个类似竞争的问题,但需要帮助来提高效率
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 我对这个程序有问题.有人看到我做错了什么吗?对此相当陌生
- 我在C++循环时做什么有什么问题?
- qt5设计器,使用fileopen,在lineedit中显示文件路径,这样做是否有问题
- 编辑:检查文件是否为空时出现问题,我做错了什么
- 撤消/重做时释放内存时出现问题
- LNK2005,我不知道我能做些什么来解决这个问题
- C++抽象类的问题(我可以用Java做,但不能用C++!)
- 我正在做一个智力竞赛游戏,我很难让第二个问题正常进行
- 我在创建对象并使用向量而不是数组使用它们的类函数时遇到问题......我做错了什么?
- 这种使用放置 new[] 有什么问题?做
- 如何做整数.权力的问题
- 在while()循环中做简单加法的问题
- Heisenbug问题与使用dll.接下来我要做什么?
- 试图编译基本Boost PropertyTree示例时出现问题-编译错误.该怎么做
- 边做边循环问题
- 视觉工作室 - 矢量问题,C++不确定我做错了什么