标签: double-precision

浮点运算太可靠了

我知道在现代计算机系统中执行的浮点运算并不总是与实际算术一致.我正在尝试设计一个小型的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)

然而,在这种情况下,xy在所述端部相等.

我是否有可能使用类似复杂度的程序演示浮点运算的不一致性,而不使用任何真正疯狂的数字?如果可能的话,我希望避免在数学上正确的值超过小数点以外的几个位置.

c# floating-point double-precision

1
推荐指数
1
解决办法
566
查看次数

C++双精度和四舍五入

我有以下问题:

double a = 6.005; double b = 5.995;

例如,我想在点之后设置2位数的精度

double c = a+b;// I would like to get 11.99 not 12.00.

我怎样才能做到这一点?

c++ double rounding double-precision

1
推荐指数
2
解决办法
7254
查看次数

精度长双倍比双重差

当我使用时,long double我的精确度比使用时更差double. 在源代码中3.14159265358979323846264L写这个是好的long double const还是我应该添加其他东西L

编辑我解决了这个问题.我更准确地改变算法.

c++ double-precision long-double

1
推荐指数
1
解决办法
256
查看次数

使用python编写双精度二进制文件

如何使用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)

python double-precision

1
推荐指数
1
解决办法
3940
查看次数

f2py 在传递给 python 时丢失双精度

我似乎无法使用 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。对我所缺少的有什么想法吗?

python precision fortran double-precision f2py

1
推荐指数
1
解决办法
464
查看次数

haskell双精度下溢与修复

我写了一些使用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)

haskell double-precision quickcheck repa

1
推荐指数
1
解决办法
144
查看次数

估计二进制文件的大小

我对此非常困惑.我转出一个大小为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字节的文件.

size fortran binaryfiles double-precision

1
推荐指数
1
解决办法
287
查看次数

是否有任何内置的.NET替代方案可以使用数字(Math.Pow)的功能而没有错误?

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替代方案可以使用数字的功能而没有错误?

.net c# double-precision pow

1
推荐指数
1
解决办法
207
查看次数

Understanding DBL_MAX

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)

c floating-point double double-precision

1
推荐指数
1
解决办法
4086
查看次数

我可以将-0.000打印为0.000吗?

printf("%.3lf\n", -0.0001);
Run Code Online (Sandbox Code Playgroud)

这输出-0.000,但不应该是0.000

如何在没有减号的情况下进行打印,即0.000

c++ double printf double-precision negative-zero

1
推荐指数
1
解决办法
611
查看次数