Do screenwriters need to consider rounding error?

I study C, and the idea of ​​guard digits and rounding errors came up. Should practicing scripting languages ​​(I think of Python and Perl here) have to worry about this? What if they do scientific programming?

+5
source share
9 answers

I would not agree with Lutz ... Although the rounding errors you mentioned exist in Python / Perl / Ruby, they have absolutely nothing to do with languages ​​implemented in C. The problem goes deeper than that.

Floating point numbers, like all data, are represented in binary form on modern computers. As well as numbers with periodic decimal representations (for example, 1/3 = 0.333333 ...), there are also numbers with periodic binary representations (for example, 1/10 = 0.0001100110011 ...). Since these numbers cannot be accurately represented in the (final sum) of computer memory, any calculations associated with them will lead to an error.

, , (.. " = 1, = 10" ), . - , , - , , .

+7

. double , , , . , , . :

use Math::BigFloat;
my $big   = Math::BigFloat->new("1_000_000_000_000_000_000_000");
my $small = Math::BigFloat->new("0.000000000000000000000000001"); 
print $big + $small;

(, , :

use bignum;
print 1_000_000_000_000_000_000_000 + 0.000000000000000000000000001

)

, :

1000000000000000000000.000000000000000000000000001

, , CPU.

+10

Python :

x = 1 / 2

. float, , C, , , float .

:

from fractions import Fraction

x = Fraction(1, 2)

.

, , , , - :

from decimal import Decimal

x = Decimal('0.5')

, , , 100 . 2.

, , , . , Python, Python , , .

, , . , , - , . --- " " . .

+6

, , , .

, 8051 , , . 1/3 1/3.

, , IEE754, IEEE754.

, , , .

:

PDL - Perl.

+4

intepreter CPython, Perl C, C.

Python SciPY NumPy .

+2

Python . " " - .

+1

, Ruby. :

irb(main):033:0> (2.01 * 1000).to_i
=> 2009
irb(main):034:0> ((2.01 * 1000.0) + 0.5).floor
=> 2010
0

, !

Python 2.6:

>>> 1440.0 / 900.0
1.6000000000000001

lutz, C, "". , , - .

0

, , , .

. , . 93 ( ). , (10 ^ -6). 50 .

- . sin() cos() . , - . sin()?

You must create an error equation to ensure that you are holding enough numbers so that the final result has a known maximum error. I do not know a “simple” numerical library that can perform this operation automatically (for example, as part of a call sin()). Here you need Matlab or something similar.

0
source

All Articles