- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图从一个变量中提取 20 位小数,但除法运算应该有错误,因为这个程序给了我一个错误的结果:
#include <iostream>
#include <cmath>
using namespace std;
int fracpart(long double input)
{
long long I;
I = input * 10;
return I % 10;
}
int main()
{
int n = 9, m = 450;
long double S;
S = (long double)n/m;
for(int i=1; i<=20; i++){
cout << fracpart(S) << " ";
S *= 10;
}
return 0;
}
我得到的:
0 1 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
我应该得到什么:
0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
最佳答案
您可以通过检查宏常量 FLT_RADIX
的值来检查浮点类型表示所使用的基数(在 header 中定义 <cfloat>
)。您可能已经知道,大多数现代计算机在内部使用二进制系统,而不是十进制系统。
现在考虑一个像 1/3 这样的有理数。它不能用基数为 10 的有限位数表示,您最终会得到一些近似值,例如 0.3333333 和一个可接受的错误。请注意,相同的数字可以在具有有限位数 (0.1) 的 3 进制系统中表示。
您尝试打印的数字 9/450 具有“不错”的以 10 为基数的表示形式 0.02,但它不能以绝对精度以 2 为基数表示,即使可以在不添加任何内容的情况下执行除法错误。不要被“2”误导,考虑 0.02 = 2/100 = 1/50 = 1/(2 * 52),其中 1/5 只能近似计算,在基数2.
无论如何,有一些方法可以实现您想要的,例如使用输出操纵器 std::setprecision
和 std::fixed
(在 header <iomanip>
中定义)甚至编写一个(非常丑陋的)自定义函数。看看这个程序的输出:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <cstdint>
// splits a number into its integral and fractional (a vector of digits) parts
std::vector<uint8_t> to_digits (
long double x, uint8_t precision, long double &integral
);
// Reconstructs the approximated number
long double from_digits (
long double integral_part, std::vector<uint8_t> &fractional_part
);
int main()
{
using std::cout;
int n = 9, m = 450;
long double S;
S = static_cast<long double>(n)/m;
cout << "\nBase 10 representation of calculated value:\n"
<< std::setprecision(70) << S << '\n';
// This ^^^^^^^^^^^^^^^^^^^^^ will change only how the value is
// printed, not its internal binary representation
cout << "\nBase 10 representation of literal:\n"
<< 0.02L << '\n';
// This ^^^^^ will print the exact same digits
// the next greater representable value is a worse approximation
cout << "\nNext representable value:\n"
<< std::nextafter(S, 1.0) << '\n';
// but you can try to obtain a "better" output
cout << "\nRounded representation printed using <iomanip> functions:\n"
<< std::setprecision(20) << std::fixed << S << '\n';
cout << "\nRounded fractional part printed using custom function:\n";
long double integral_part;
auto dd = to_digits(S, 20, integral_part);
for (auto const d : dd)
{
cout << static_cast<int>(d);
}
cout << '\n';
// Reversing the process...
cout << "\nApproximated value (using custom function):\n";
auto X = from_digits(integral_part, dd);
cout << std::setprecision(70) << std::fixed << X << '\n';
cout << std::setprecision(20) << std::fixed << X << '\n';
}
std::vector<uint8_t> to_digits (
long double x, uint8_t precision, long double &integral
)
{
std::vector<uint8_t> digits;
long double fractional = std::modf(x, &integral);
for ( uint8_t i = 0; i < precision; ++i )
{
long double digit;
fractional = std::modf(fractional * 10, &digit);
digits.push_back(digit);
}
if ( digits.size() && std::round(fractional) == 1.0L )
{
uint8_t i = digits.size();
while ( i )
{
--i;
if ( digits[i] < 9 )
{
++digits[i];
break;
}
digits[i] = 0;
if ( i == 0 )
{
integral += 1.0L;
break;
}
}
}
return digits;
}
long double from_digits (
long double integral_part, std::vector<uint8_t> &fractional_part
)
{
long double x = 1.0L;
for ( auto d : fractional_part )
{
x *= 10.0L;
integral_part += d / x;
}
return integral_part;
}
关于C++除小数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46348802/
如何将整数类型转换为 double /浮点类型以显示小数点?例如,如果我想将数字转换为货币格式: 5 会变成 5.004.3 会变成 4.30 javascript 有什么东西可以用来做这种转换吗?
【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/cnb-yuchen/p/18107586 出自【进步*于辰的博客】
我意识到这是一个重复的问题,但是 this 中提到的解决方案这个问题完全不适合我。 我目前的代码如下 Sub ConvertTextToNumber() Dim Area As Range, C As
我正在使用数学 javascript,但在用它来替换点的逗号和逗号的点时遇到了一些麻烦。我可以改变千位分隔符的逗号,但无法设法将小数点变成逗号。我尝试了其他帖子中的一些建议,但没有感到高兴。目标是实现
我正在尝试在 Android 中创建一个数字选择器,但轮子只增加 1。我想增加 0.1。我在网上查了一下,但我发现了一个格式化的浮点数组禁用了轮子。请帮助并为语法感到抱歉,我正在学习。 最佳答案 您可
我正在尝试在多个网站上获取利率。数据相当非结构化,但形式足够接近。我想要捕捉的内容: x.xx% 至 xx.xx% 数据示例: 由 FDIC 成员 WebBank 发放的所有贷款。您的实际利率取决于信
在 MySQL 表中,我有一个具有不同值的 VARCHAR 列,这些值可能代表字符串、整数、浮点、任意值。这些值作为特定于语言的字符串写入数据库,这意味着 123.45 的浮点值可以写为德语中的 "1
我想编写一个正则表达式,它允许整数或具有 0 - 2 个小数位的小数。 有效输入 1 1. 1.1 1.11 111111111 111111111. 111111111.1 111111111.11
我正在尝试为 nullable 实现客户端验证其小数点分隔符可以是逗号(例如:123,45)。 在我看来: ... @Html.LabelFor(model => model.Turnove
我找不到合适的正则表达式来仅从字符串中提取 float 。考虑以下字符串: $string = "8x2.1 3x2"; 我想提取 2.1,我尝试了以下操作,但这给了我整数和 float : preg
我希望使用正则表达式函数分离以下数据,如下所示: 要使用的功能: let fx=(text,regex)=> Web.Page( " var x='
我是 jquery 新手。我有一个带有两个输入框的表单。我实现了一些验证。 Min.Amount Max.Amount
我正在java中实现一个简单的算法,它接受一个整数数组,并查找并返回数组中相邻整数的最大乘积。 为此,我首先初始化了一个名为largestProduct的变量,我用它来跟踪当前找到的最大(最佳)产品。
在 JavaScript 中,我想定义小数点的位置。我只能在示例中真正展示它。 假设输入值为 1234 。 我希望输出为 123.4 。 或者,如果输入是 12345 ,我希望输出是 123.45 。
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我目前正在开发一些基于 Django 的 Web 项目,在这个 Web 开发过程中,我遇到了以下我无法正确理解的代码。 if price_product['price'] == Decimal('-1
这个问题在这里已经有了答案: How do I print a double value with full precision using cout? (17 个答案) 关闭 7 年前。 我试图在
这应该是微不足道的,但我正在兜圈子,也许有人可以提供帮助。 我有两个表(T1,T2),我希望从中提取每行中的多个值并更新第三个表(T3)的内容当且仅当)T1 中有两个 UQ,NN 字段,T2 匹配,在
如果数字不是十进制,我需要附加.00,但是当我尝试下面的代码时,它会将整个数字更改为0.00。例如,如果数字是12,200,它会将其更改为0.00,而不是在末尾添加.00 $('.total-amou
我正在尝试在容器 View 中设置 9:16 纵横比 View 。以下代码在 viewDidLayoutSubviews 中设置约束,以便在正确的位置考虑自动布局。它还调用 layoutIfNeede
我是一名优秀的程序员,十分优秀!