请考虑以下代码:
0.1 + 0.2 == 0.3 -> false
Run Code Online (Sandbox Code Playgroud)
0.1 + 0.2 -> 0.30000000000000004
Run Code Online (Sandbox Code Playgroud)
为什么会出现这些不准确之处?
public class doublePrecision {
public static void main(String[] args) {
double total = 0;
total += 5.6;
total += 5.8;
System.out.println(total);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印:
11.399999999999
Run Code Online (Sandbox Code Playgroud)
我怎么才能打印(或能够用它)11.4?
可能重复:
浮点数是如何存储的?什么时候重要?
为什么Python解释器中会出现以下情况?
>>> 0.1+0.1+0.1-0.3
5.551115123125783e-17
>>> 0.1+0.1
0.2
>>> 0.2+0.1
0.30000000000000004
>>> 0.3-0.3
0.0
>>> 0.2+0.1
0.30000000000000004
>>>
Run Code Online (Sandbox Code Playgroud)
为什么不0.2 + 0.1 = 0.3呢?
也许,这是一个非常简单的问题,但我无法得到答案.我已经搜索了很长一段时间(现在谷歌认为我正在发送自动查询http://twitter.com/michaelsync/status/17177278608)..
int n = 4.35 *100;
cout << n;
Run Code Online (Sandbox Code Playgroud)
为什么输出变为"434"而不是"435"?4.35*100 = 435这是一个整数值,这应该可以分配给整数变量"n",对吗?
或者C++编译器在乘法之前是否将4.35转换为整数?我认为不会.为什么编译器会自动将4.35更改为4.34,这仍然是一个浮点数?
谢谢.
我今天参加了C考试,我被问到类似的问题:
这个程序有什么问题:
Run Code Online (Sandbox Code Playgroud)for( x = .1 ; x != 1.0 ; x += .1) printf("%f\n", x);
我无法解决它,因为我必须写一些我标记.1为错误的东西.但是,当我回到家里时,我运行了这个程序,事实证明它在x等于1.0并陷入无限循环时不会中断:
$ cat exam.c
#include <stdio.h>
int main(int argc, char **argv)
{
float x;
for(x = .1 ; x != 1.0 ; x += .1)
printf("%f\n", x);
return 0;
}
$ gcc exam.c -o exam
$ ./exam
0.100000
0.200000
0.300000
0.400000
0.500000
0.600000
0.700000
0.800000
0.900000
1.000000 <- ?
1.100000
1.200000
1.300000
1.400000
1.500000
....
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会发生这种情况.
码:
var tax= 14900*(0.108);
alert(tax);
Run Code Online (Sandbox Code Playgroud)
以上给出了1609.2的答案
var tax1= 14900*(10.8/100);
alert(tax1);
Run Code Online (Sandbox Code Playgroud)
以上给出了1609.200000000003的答案
为什么?我想我可以收集价值观,但为什么会这样呢?
更新: 找到问题的临时解决方案.
首先乘以:
(14900*10.8)/100 = 1609.2
Run Code Online (Sandbox Code Playgroud)
然而
(14898*10.8)/100 = 1608.9840000000002
Run Code Online (Sandbox Code Playgroud)
为此,将10.8乘以a factor(100 in this case)并调整分母:
(14898*(10.8*100))/10000 = 1608.984
Run Code Online (Sandbox Code Playgroud)
我想如果可以为额外的000s做一个preg_match然后相应地调整因子,可以避免浮点错误.然而,最终的解决方案是数学库.
我试图在C中打印出浮点数的每一位.
我能够用这个整数做到这一点:
int bit_return(int a, int loc)
// Bit returned at location
{
int buf = a & 1<<loc;
if (buf == 0)
return 0;
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
如果我替换int a为编译器将无法编译float a.
这有解决方案吗?
复制并重新格式化您的评论如下
好的,对于不清楚的人,我在这里发布我的整个代码:
#include <stdio.h>
#include <stdlib.h>
int bit_return(int a, int loc) // Bit returned at location
{
int buf = a & 1<<loc;
if (buf == 0)
return 0;
else
return 1;
}
int main()
{
int a = 289642; // Represent 'a' …Run Code Online (Sandbox Code Playgroud) 在所有其他语言(一般算术引擎)中,在相同优先级的运算符周围放置一组额外的括号不会影响结果.但最近在测试项目中,我注意到MS SQL服务器在这些情况下更改了结果.请查看下面的查询,如果您有任何想法(或SQL Server管理中的设置)或任何解释该行为的MSDN文章链接,请告诉我.
select (0.55 * 287.61 / 0.66) calc_no_parens
,(0.55 * (287.61 / 0.66)) calc_parens
,round(0.55 * 287.61 / 0.66,2) no_paren_round
,round(0.55 * (287.61 / 0.66),2) paren_round;
Run Code Online (Sandbox Code Playgroud)
结果
Column Record 1
calc_no_parens 239.6750000
calc_parens 239.67499985
no_paren_round 239.6800000
paren_round 239.67000000
Run Code Online (Sandbox Code Playgroud)
对我来说,前两个应该返回239.675,圆形应该给239.68.
在java中我使用float来存储数字.我选择浮点格式,因为我使用整数和双数字,其中数字不同,可能有大整数或大的双数字,小数位数不同.但是当我将这些数字插入数据库时,会存储错误的数字.例如:
float value = 0f;
value = 67522665;
System.out.println(value);
印刷版:6.7522664E7,它作为67522664存储在数据库中,而不是67522665
float totalAmount = 0;
.
.
.//totalAmount assigned value 1.05 correctly
.
totalAmount += float.Parse(dataRow["Amt"].ToString()); //where dataRow["Amt"] has value 4.93
Run Code Online (Sandbox Code Playgroud)
我得到的totalAmount的答案是5.97999954而不是5.98
为什么会这样?
我已经看到术语“float”和“double”应用于不同场景的示例,并且似乎理解位和字节起到了某种作用,但我找不到关于差异的明确解释。内存限制似乎是造成这种差异的原因(??),但我真的只是想具体知道一个与另一个是什么。
此外,如何判断数据是否被分类(或应该存储)为“float”还是“double”?
这些概念如何应用于十进制值(相对于二进制)?
上下文:我在 .csv 表中存储了一系列变量,我试图为其定义“存储类型”。有些是整数,有些是字符串,还有一些是带小数的数字。我只是想弄清楚如何为每个定义“存储类型”。
那么,我有这两个问题吗?
#include <stdio.h>
int main(void)
{
float a = 0.7;
if (0.7 > a)
printf("Hi\n");
else
printf("Hello\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这将是什么输出?据我说,它应该是"你好".
其次,我有,
#include <stdio.h>
int main(void)
{
int a=500,b=100,c;
if (!a >= 400)
b=300;
c=200;
printf("%d %d\n",b,c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,输出应该是,
100 200
Run Code Online (Sandbox Code Playgroud)
因为!一个手段,而不是一个手段,这意味着,一个非500的值与400相比,它可以大于或小于400,那么为什么它会是300?
c ×3
math ×3
double ×2
java ×2
precision ×2
c# ×1
c++ ×1
data-storage ×1
decimal ×1
javascript ×1
python ×1
python-3.x ×1
sql ×1
sql-server ×1
storage ×1