我知道在现代计算机系统中执行的浮点运算并不总是与实际算术一致.我正在尝试设计一个小型的C#程序来演示这一点.例如:
static void Main(string[] args)
{
double x = 0, y = 0;
x += 20013.8;
x += 20012.7;
y += 10016.4;
y += 30010.1;
Console.WriteLine("Result: "+ x + " " + y + " " + (x==y));
Console.Write("Press any key to continue . . . "); Console.ReadKey(true);
}
Run Code Online (Sandbox Code Playgroud)
然而,在这种情况下,x和y在所述端部相等.
我是否有可能使用类似复杂度的程序演示浮点运算的不一致性,而不使用任何真正疯狂的数字?如果可能的话,我希望避免在数学上正确的值超过小数点以外的几个位置.
我有以下问题:
double a = 6.005; double b = 5.995;
例如,我想在点之后设置2位数的精度
double c = a+b;// I would like to get 11.99 not 12.00.
我怎样才能做到这一点?
当我使用时,long double我的精确度比使用时更差double.
在源代码中3.14159265358979323846264L写这个是好的long double const还是我应该添加其他东西L?
编辑我解决了这个问题.我更准确地改变算法.
如何使用Python编写双精度文件二进制文件?我现在正在做以下事情,但它给了我单精度文件:
#!/usr/bin/env python
import struct
data =[2.3715231753176,9.342983274982732]
output_file = "test.dat"
out_file = open(output_file,"wb")
s = struct.pack('f'*len(data), *data)
out_file.write(s)
out_file.close()
Run Code Online (Sandbox Code Playgroud) 我似乎无法使用 f2py 创建模块来将数字传回 python 时保持双精度。一个最小的例子,带有文件 fmodules.f90:
subroutine example(output)
implicit none
double precision :: output
cf2py intent(out) :: output
output = 1.3
end subroutine example
Run Code Online (Sandbox Code Playgroud)
然后我使用 f2py 创建模块:
$ f2py -c -m fmodules fmodules.f90
Run Code Online (Sandbox Code Playgroud)
并从 python 中调用它:
>>> from fmodules import example
>>> example()
1.2999999523162842
Run Code Online (Sandbox Code Playgroud)
根据我的计算,精度约为 8 位数字。我的印象是双精度应该给出大约 16。我已经尝试了我能想到的所有排列,包括处理文件.f2py_f2cmap。对我所缺少的有什么想法吗?
我写了一些使用repa 计算距离矩阵的代码:
distance :: Int -> Int -> Mat -> Double
distance aindx bindx arr = let a = slice arr (Any :. aindx :. All)
b = slice arr (Any :. bindx :. All)-
sqdiff = R.map (\x -> x*x) $ R.zipWith (-) a b
in sqrt $ sumAllS sqdiff
buildDistanceMatrix :: Mat -> Mat
buildDistanceMatrix m = let (Z :. height :. width) = R.extent m
cords = fromListUnboxed (Z :. (height * height) ) [ (x,y) …Run Code Online (Sandbox Code Playgroud) 我对此非常困惑.我转出一个大小为16000*4*2的3D数组,其中所有元素都是DOUBLE PRECISION,我认为我应该得到一个大小为16000*4*2*8字节/ dp = 1,024,000字节的文件.但我一直得到2,048,000字节.
我测试了一个简单的测试程序:
PROGRAM testprog
IMPLICIT NONE
DOUBLE PRECISION :: x=0.0D0
INTEGER :: i
OPEN(UNIT=128,FILE='try.out',FORM='UNFORMATTED',ACCESS='SEQUENTIAL')
DO i=1,16000*4*2
WRITE(128) x
ENDDO
CLOSE(128)
ENDPROGRAM testprog
Run Code Online (Sandbox Code Playgroud)
并使用以下命令运行它:
gfortran f.f90 -o a
./a
ls -als try.out
Run Code Online (Sandbox Code Playgroud)
而我得到的是
2000 -rw-r--r-- 1 jipuwang umstudents 2048000 Dec 17 20:16 try.out
Run Code Online (Sandbox Code Playgroud)
我无法理解它.一个双精度使用2个字节对吗?
我做了别的事,如果有人也能帮我理解这个:
PROGRAM testprog
IMPLICIT NONE
DOUBLE PRECISION :: x=0.0D0
INTEGER :: i
OPEN(UNIT=128,FILE='try.out')
DO i=1,2
WRITE(128,*) x
ENDDO
CLOSE(128)
ENDPROGRAM testprog
Run Code Online (Sandbox Code Playgroud)
它给了我一个大小为54字节的文件.
Math.Pow似乎无法正常工作以取得重大成果.可能是因为它double用于计算(如何在.NET Framework中实现Math.Pow()?).
例如:
public static void Main()
{
Console.WriteLine((long)Math.Pow(17, 13));
Console.WriteLine(Pow(17, 13));
}
public static long Pow(int num, int pow)
{
long answer = 1;
for (int i = 0; i < pow; i++)
{
answer *= num;
}
return answer;
}
Run Code Online (Sandbox Code Playgroud)
上面代码的结果是:
9904578032905936
9904578032905937
Run Code Online (Sandbox Code Playgroud)
是否有任何内置的.NET替代方案可以使用数字的功能而没有错误?
I just read about the IEEE 754 standard in order to understand how single-precision and double-precision floating points are implemented.
So I wrote this to check my understanding:
#include <stdio.h>
#include <float.h>
int main() {
double foo = 9007199254740992; // 2^53
double bar = 9007199254740993; // 2^53 + 1
printf("%d\n\n", sizeof(double)); // Outputs 8. Good
printf("%f\n\n", foo); // 9007199254740992.000000. Ok
printf("%f\n", bar); // 9007199254740992.000000. Ok because Mantissa is 52 bits
printf("%f\n\n", DBL_MAX); // ??
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Output:
8
9007199254740992.000000 …Run Code Online (Sandbox Code Playgroud) printf("%.3lf\n", -0.0001);
Run Code Online (Sandbox Code Playgroud)
这输出-0.000,但不应该是0.000?
如何在没有减号的情况下进行打印,即0.000?
double-precision ×10
c++ ×3
double ×3
c# ×2
fortran ×2
python ×2
.net ×1
binaryfiles ×1
c ×1
f2py ×1
haskell ×1
long-double ×1
pow ×1
precision ×1
printf ×1
quickcheck ×1
repa ×1
rounding ×1
size ×1