您好我知道计算机受数字精度的限制,有些数字无法在二进制系统中准确表示.这就是我问为什么这样比较的原因
(0.1*3) == 0.3
Run Code Online (Sandbox Code Playgroud)
在Java语言中评估为false在C中它评估为true,对我来说Java行为更直观一些.任何提供的答案都不能回答我的问题,为什么他们有不同的行为,因为两者都使用IEEE 754标准.
@update我正在使用ideone来测试条件.
想象一下10辆车随机,均匀分布在一个长度为1的圆形轨道上。如果位置用[0,1>范围内的C double表示,那么它们可以排序,车之间的间隙应该是车的位置前面减去后面汽车的位置。最后一个间隙需要添加 1 来解释不连续性。
在程序输出中,最后一列的统计数据和分布与其他列有很大不同。行正确地加到 1。这是怎么回事?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int compare (const void * a, const void * b)
{
if (*(double*)a > *(double*)b) return 1;
else if (*(double*)a < *(double*)b) return -1;
else return 0;
}
double grand_f_0_1(){
static FILE * fp = NULL;
uint64_t bits;
if(fp == NULL) fp = fopen("/dev/urandom", "r");
fread(&bits, sizeof(bits), 1, fp);
return (double)bits * 5.421010862427522170037264004349e-020; // https://stackoverflow.com/a/26867455
}
int main()
{
const int n = 10;
double values[n];
double diffs[n]; …Run Code Online (Sandbox Code Playgroud) 我有两个用于存储地理位置数据的字段,在我的MySQL数据库中定义为双精度数:
`address_geo_latitude` float(10,6) NOT NULL,
`address_geo_longitude` float(10,6) NOT NULL
Run Code Online (Sandbox Code Playgroud)
我正在使用Yii2的double验证器而不是用户传递的值:
public function rules()
{
return [
[['address_geo_latitude', 'address_geo_longitude'], 'double', 'min'=>0, 'max'=>360]
];
}
Run Code Online (Sandbox Code Playgroud)
(虽然我的测试似乎证明,这个问题与Yii2验证器无关)
在测试期间,我观察到值的奇怪(?)变化,即:
359.90成为359.899994(0,000006差异),359.80成为359.799988(0,000012差异),311.11成为311.109985(0,000015差异),255.55成为255.550003(-0,000003差异),205.205成为205.205002(-0,000002差异),105.105变得105.105003(-0,000003差异).但:
359.899994遗体359.899994,311.109985遗体311.109985,311遗体311,255遗体255, …我想将一个字符串转换为double并保持相同的值:
let myStr = "2.40"
let numberFormatter = NSNumberFormatter()
numberFormatter.locale = NSLocale(localeIdentifier: "fr_FR")
let myDouble = numberFormatter.numberFromString(myStr)?.doubleValue ?? 0.0
Run Code Online (Sandbox Code Playgroud)
myDouble现在
Double? 2.3999999999999999
Run Code Online (Sandbox Code Playgroud)
那么如何将"2.40"转换为精确到2.40为双?
更新:
即使转换后舍入也似乎不起作用
我不想打印,我想计算,重要的是数字应该是正确的,这是货币计算和费率
刚开始学习C#.我计划将它用于繁重的数学模拟,包括数值求解.问题是我在添加和减去double时以及比较时都会出现精度损失.代码及其返回的内容(在评论中)如下:
namespace ex3
{
class Program
{
static void Main(string[] args)
{
double x = 1e-20, foo = 4.0;
Console.WriteLine((x + foo)); // prints 4
Console.WriteLine((x - foo)); // prints -4
Console.WriteLine((x + foo)==foo); // prints True BUT THIS IS FALSE!!!
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助和澄清!
令我困惑的是(x + foo)==foo回报True.
我想发送一个仅用两个小数位浮点数创建的NSNumber。
所以我要创建一个带有2个小数位数的NSNumberFormatter,但是它不尊重小数位数的数量。
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumFractionDigits = 2;
formatter.roundingMode = NSNumberFormatterRoundUp;
NSString *preformatedPreAuthPrice = [formatter stringFromNumber:@(4.44)];
self.PreAuthPrice=[formatter numberFromString:preformatedPreAuthPrice];
Run Code Online (Sandbox Code Playgroud)
当我打印NSNumber的类型时,它是一个两倍
p strcmp([(NSNumber *)[dictionary objectForKey:@"PreAuthPrice"] objCType], @encode(double))
(int) $1 = 0
Run Code Online (Sandbox Code Playgroud)
如果我得到了价值:
po [self.PreAuthPrice doubleValue]
4.4400000000000004
Run Code Online (Sandbox Code Playgroud)
即使我这样做:
po [@(4.44) doubleValue]
4.4400000000000004
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我在iOS 10上发送此NSNumber(使用AFNetworking版本2.6,3和3.1.0)时,它发送了正确的值4.44。但是在iOS 11上它会发送4.4400000000000004
这是查尔斯捕获的:
iOS 10
{
"preAuthPrice": 4.44,
}
Run Code Online (Sandbox Code Playgroud)
iOS 11
{
"preAuthPrice": 4.4400000000000004
}
Run Code Online (Sandbox Code Playgroud)
我也在基础库的发行说明中找到了这一点:
NS编号
迅速将NSNumber桥接到数字类型现在统一限制了as的情况?无论NSNumber是如何创建的,都进行转换。如果不能将数字安全地表示为目标类型,则将NSNumbers转换为数字类型将返回nil。
这发生在任何人身上吗?可能与发行说明有关吗?在iOS 10中发送[self.PreAuthPrice stringValue],在iOS 11中发送[self.PreAuthPrice doubleValue]
我知道一个解决方案是将值作为字符串发送,并且在阅读后也知道:
和
我知道这可能是问题所在,但是为什么要使用iOS 10。
我有一个问题,即表示[0,1]区间总和的概率的变量之和0应该是>0.问题肯定与浮点表示和精度有关R,但我无法确定它出错的地方.
options(digits = 22)
p1 = 0.8
p2 = 0.9999999999999998
p11 = p1 * p2
p10 = p1 - p11
p01 = p2 - p11
p00 = 1 - p11 - p10 - p01
Run Code Online (Sandbox Code Playgroud)
p11, p10, p01都是numeric.p00也numeric不过
> p00
[1] 0
Run Code Online (Sandbox Code Playgroud)
和p00 == 0是TRUE我的机器上.但是它不应该是零,因为它可以证明p00是>0数学.
这个问题似乎与p01小的事实有关.不过p01>0是TRUE仍然适用我的机器上.为什么在拿到最后一笔钱时会出错p00?
是否有一个数字技巧来解决这个问题,即得到一个精确的表示p00 …
转换0 10000101 01010011000000000000001为十进制(这是84.75000762939453125)效果很好:
FP: 0 10000101 01010011000000000000001
Fraction: (1.)01010011000000000000001
Exp 10000101: 133
Bias: 133-127=6
Moving by exp: 1010100.11000000000000001
Convert parts to decimal: 84.75000762939453125
Run Code Online (Sandbox Code Playgroud)
为什么我无法将相同的转换0 00000001 00000000000000000000001为十进制(即(1.1754945E-38):
FP: 0 00000001 00000000000000000000001
Fraction: (1.)00000000000000000000001
Exp 00000001: 1
Bias: 1-127=-126
Moving by exp: 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
Convert parts to decimal: 0.?!?!?!
Run Code Online (Sandbox Code Playgroud)
无法1.1754945E-38将二进制转换0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001为十进制.
我哪里错了?
有人可以解释一下这里发生了什么吗?
为什么 0.3 和 0.7 的值有更多的小数点?我只想要 1 位小数点值。
threshold_range = np.arange(0.1,1,0.1)
threshold_range.tolist()
Run Code Online (Sandbox Code Playgroud)
[Output]: [0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7000000000000001, 0.8, 0.9]
Run Code Online (Sandbox Code Playgroud) 我有 R 版本 4.1.2 (2021-11-01)。
trunc()当输入数字有大量小数值时,似乎功能不一致。
trunc(3.99999999999999977799999999999999999999900)
[1] 4
trunc(3.999999999999999777999999999999999999999000)
[1] 3
Run Code Online (Sandbox Code Playgroud)
或者
trunc(3.9999999999999997778888888888880)
[1] 4
trunc(3.999999999999999777888888888888)
[1] 3
Run Code Online (Sandbox Code Playgroud)
我不确定是什么导致了这种不一致?