C例程将浮子绕到n个重要数字
A C routine to round a float to n significant digits?
假设我有一个float
。我想将其舍入一定数量的大数字。
在我的情况下n=6
。
所以说float是 f=1.23456999;
round(f,6)
将提供1.23457
f=123456.0001
将提供123456
有人知道这样的例行程序吗?
在这里它在网站上工作:http://ostermiller.org/calc/significant_figures.html
将数字乘以合适的缩放系数,以将所有有意义的数字移动到小数点的左侧。然后圆,最后逆转操作:
#include <math.h>
double round_to_digits(double value, int digits)
{
if (value == 0.0) // otherwise it will return 'nan' due to the log10() of zero
return 0.0;
double factor = pow(10.0, digits - ceil(log10(fabs(value))));
return round(value * factor) / factor;
}
测试:http://ideone.com/fh5ebt
buts @pascalcuoq指出:圆形值可能无法完全表示为浮点值。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *Round(float f, int d)
{
char buf[16];
sprintf(buf, "%.*g", d, f);
return strdup(buf);
}
int main(void)
{
char *r = Round(1.23456999, 6);
printf("%sn", r);
free(r);
}
输出是:
1.23457
类似的东西应该有效:
double round_to_n_digits(double x, int n)
{
double scale = pow(10.0, ceil(log10(fabs(x))) + n);
return round(x * scale) / scale;
}
另外,您可以只使用sprintf
/atof
转换为字符串,然后再次返回:
double round_to_n_digits(double x, int n)
{
char buff[32];
sprintf(buff, "%.*g", n, x);
return atof(buff);
}
上述两个功能的测试代码:http://ideone.com/omzqzz
请注意,在某些情况下可能会观察到不正确的舍入,例如正如@clearscreen在下面的评论中指出的那样,13127.15舍入至13127.1而不是13127.2。
这应该有效(除了浮点精度给出的噪声外):
#include <stdio.h>
#include <math.h>
double dround(double a, int ndigits);
double dround(double a, int ndigits) {
int exp_base10 = round(log10(a));
double man_base10 = a*pow(10.0,-exp_base10);
double factor = pow(10.0,-ndigits+1);
double truncated_man_base10 = man_base10 - fmod(man_base10,factor);
double rounded_remainder = fmod(man_base10,factor)/factor;
rounded_remainder = rounded_remainder > 0.5 ? 1.0*factor : 0.0;
return (truncated_man_base10 + rounded_remainder)*pow(10.0,exp_base10) ;
}
int main() {
double a = 1.23456999;
double b = 123456.0001;
printf("%12.12fn",dround(a,6));
printf("%12.12fn",dround(b,6));
return 0;
}
如果要将浮子打印到字符串,请使用简单的sprintf()
。为了将其输出到控制台,您可以使用printf()
:
printf("My float is %.6f", myfloat);
这将用6个小数位置输出您的浮标。
打印到16个重要数字。
double x = -1932970.8299999994;
char buff[100];
snprintf(buff, sizeof(buff), "%.16g", x);
std::string buffAsStdStr = buff;
std::cout << std::endl << buffAsStdStr ;
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- c++类声明时,相同的例程,不同的成员变量类型
- C++为线程工作动态地分割例程
- 子例程,不使用 pow,并带有参数和返回
- 直接在RcppArmadillo中调用LAPACK例程
- 如何将C++子例程链接到 x86 程序集程序?
- PX 转换例程编译问题
- 成功完成TLS握手后,服务器关闭时出现错误的SSL例程:SSL3_GET_RECORD:错误的版本号
- 只允许授权代码调用库中的例程
- JNI 不满意链接错误: 动态链接库 (DLL) 初始化例程失败
- 调用子例程时类型不匹配
- 将分配给C++数组传递给 Fortran 子例程
- Android Studio 3.1.2 - 无法运行C++子例程"No implementation found for Java.lang.String..."
- Posix 线程类和启动例程 (pthread)
- OpenBLAS 只为一个例程设置线程数
- 检查并行化 BLAS 例程的结果
- 我们应该测量例程的平均执行时间还是最小执行时间?
- 给定n个数字,编写一个例程,以在4个连续数字之间找到最大的数字
- C例程将浮子绕到n个重要数字
- 数字信号处理例程中的Node.js