我知道0.1十进制数不能用有限的二进制数(解释)精确表示,所以double n = 0.1会失去一些精度而不会完全正确0.1.另一方面0.5可以完全表示,因为它是0.5 = 1/2 = 0.1b.
已经说过,添加0.1 三次不会完全给出0.3以下代码打印是可以理解的false:
double sum = 0, d = 0.1;
for (int i = 0; i < 3; i++)
sum += d;
System.out.println(sum == 0.3); // Prints false, OK
Run Code Online (Sandbox Code Playgroud)
但是,如何增加0.1 五次才会给出确切的答案0.5呢?以下代码打印true:
double sum = 0, d = 0.1;
for (int i = 0; i < 5; i++)
sum += …Run Code Online (Sandbox Code Playgroud) 代码
float x = 3.141592653589793238;
double z = 3.141592653589793238;
printf("x=%f\n", x);
printf("z=%f\n", z);
printf("x=%20.18f\n", x);
printf("z=%20.18f\n", z);
Run Code Online (Sandbox Code Playgroud)
会给你输出
x=3.141593
z=3.141593
x=3.141592741012573242
z=3.141592653589793116
Run Code Online (Sandbox Code Playgroud)
输出的第三行741012573242是垃圾,第四行116是垃圾.双打总是有16个有效数字,而浮点数总是有7个有效数字吗?为什么双打没有14位重要人物?
我想在PHP中比较两个浮点数,如下面的示例代码:
$a = 0.17;
$b = 1 - 0.83; //0.17
if($a == $b ){
echo 'a and b are same';
}
else {
echo 'a and b are not same';
}
Run Code Online (Sandbox Code Playgroud)
在此代码它返回的结果else条件,而不是if条件,即使$a和$b相同.在PHP中有没有特殊的方法来处理/比较浮点数?
如果是,那么请帮我解决这个问题.
或者我的服务器配置有问题吗?
我理解gcc的--ffast-math标志可以大大提高浮动操作的速度,并超出IEEE标准,但我似乎无法找到有关它正在发生的事情的信息.任何人都可以解释一些细节,并可能给出一个明确的例子,说明如果标志开启或关闭会有什么变化?
我确实尝试过挖掘SO以寻找类似的问题,但却找不到任何解释ffast-math工作原理的东西.
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?
在以下示例中是否可以将除以0(或无穷大)?
public double calculation(double a, double b)
{
if (a == b)
{
return 0;
}
else
{
return 2 / (a - b);
}
}
Run Code Online (Sandbox Code Playgroud)
在正常情况下,它当然不会.但是,如果a并且b非常接近,可能会(a-b)导致0计算的精确性?
请注意,这个问题适用于Java,但我认为它适用于大多数编程语言.
我说有一个浮点数135.12345678910.我想将该值连接到字符串,但只想要135.123456789.通过打印,我可以通过以下方式轻松完成此操作:
print "%.9f" % numvar
Run Code Online (Sandbox Code Playgroud)
与numvar我的原始号码.是否有捷径可寻?
我有以下简单的代码:
int speed1 = (int)(6.2f * 10);
float tmp = 6.2f * 10;
int speed2 = (int)tmp;
Run Code Online (Sandbox Code Playgroud)
speed1和speed2应该具有相同的值,但事实上,我有:
speed1 = 61
speed2 = 62
Run Code Online (Sandbox Code Playgroud)
我知道我应该使用Math.Round而不是cast,但我想了解为什么值不同.
我查看了生成的字节码,但除了存储和加载外,操作码是相同的.
我也在java中尝试了相同的代码,我正确地获得了62和62.
有人可以解释一下吗?
编辑: 在实际代码中,它不是直接6.2f*10而是函数调用*常量.我有以下字节码:
速度1:
IL_01b3: ldloc.s V_8
IL_01b5: callvirt instance float32 myPackage.MyClass::getSpeed()
IL_01ba: ldc.r4 10.
IL_01bf: mul
IL_01c0: conv.i4
IL_01c1: stloc.s V_9
Run Code Online (Sandbox Code Playgroud)
速度2:
IL_01c3: ldloc.s V_8
IL_01c5: callvirt instance float32 myPackage.MyClass::getSpeed()
IL_01ca: ldc.r4 10.
IL_01cf: mul
IL_01d0: stloc.s V_10
IL_01d2: ldloc.s V_10
IL_01d4: conv.i4
IL_01d5: stloc.s V_11
Run Code Online (Sandbox Code Playgroud)
我们可以看到操作数是浮点数,唯一的区别是stloc/ldloc
至于虚拟机,我尝试使用Mono/Win7,Mono/MacOS和.NET/Windows,结果相同
如何修改pandas中groupby操作的输出格式,为大数字生成科学记数法.我知道如何在python中进行字符串格式化,但是在这里应用它时我感到很茫然.
df1.groupby('dept')['data1'].sum()
dept
value1 1.192433e+08
value2 1.293066e+08
value3 1.077142e+08
Run Code Online (Sandbox Code Playgroud)
如果我转换为字符串,这会抑制科学记数法,但现在我只是想知道如何字符串格式和添加小数.
sum_sales_dept.astype(str)
Run Code Online (Sandbox Code Playgroud) python floating-point scientific-notation number-formatting pandas
为什么Clang会优化此代码中的循环
#include <time.h>
#include <stdio.h>
static size_t const N = 1 << 27;
static double arr[N] = { /* initialize to zero */ };
int main()
{
clock_t const start = clock();
for (int i = 0; i < N; ++i) { arr[i] *= 1.0; }
printf("%u ms\n", (unsigned)(clock() - start) * 1000 / CLOCKS_PER_SEC);
}
Run Code Online (Sandbox Code Playgroud)
但不是这段代码中的循环?
#include <time.h>
#include <stdio.h>
static size_t const N = 1 << 27;
static double arr[N] = { /* initialize to zero */ }; …Run Code Online (Sandbox Code Playgroud)