国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

0.1+0.2!=0.3

2019-11-06 06:41:45
字體:
來源:轉載
供稿:網友
Floating Point MathYour language isn't broken, it's doing floating point math. Computers can only natively store integers, so they need some way of rePResenting decimal numbers. This representation comes with some degree of inaccuracy. That's why, more often than not, .1 + .2 != .3.Why does this happen?It's actually pretty simple. When you have a base 10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7. In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals. While, 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is Operating in. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer's base 2 (binary) number into a more human readable base 10 number.Below are some examples of sending.1 + .2 to standard output in a variety of languages.
LanguageCodeResult
C#include<stdio.h> int main(int argc, char** argv) { printf("%.17f/n", .1+.2); return 0; }0.30000000000000004
C++#include <iomanip> std::cout << setprecision(17) << 0.1 + 0.2 << std.endl;0.30000000000000004
phpecho .1 + .2;0.3
PHP converts 0.30000000000000004 to a string and shortens it to "0.3". To achieve the desired floating point result, adjust the precision ini setting: ini_set("precision", 17).
MySQLSELECT .1 + .2;0.3
PostgresSELECT select 0.1::float + 0.2::float;0.3
Delphi XE5writeln(0.1 + 0.2);3.00000000000000E-0001
Erlangio:format("~w~n", [0.1 + 0.2]).0.30000000000000004
ElixirIO.puts(0.1 + 0.2)0.30000000000000004
Rubyputs 0.1 + 0.2Andputs 1/10r + 2/10r0.30000000000000004And3/10
Ruby supports rational numbers in syntax with version 2.1 and newer directly. For older versions use Rational.Ruby also has a library specifically for decimals: BigDecimal.
Python 2print(.1 + .2)Andfloat(decimal.Decimal(".1") + decimal.Decimal(".2")) And.1 + .20.3And0.3And0.30000000000000004
Python 2's "print" statement converts 0.30000000000000004 to a string and shortens it to "0.3". To achieve the desired floating point result, use print(repr(.1 + .2)). This was fixed in Python 3 (see below).
Python 3print(.1 + .2)And.1 + .20.30000000000000004And0.30000000000000004
Luaprint(.1 + .2)print(string.format("%0.17f", 0.1 + 0.2))0.30.30000000000000004
javaScriptdocument.writeln(.1 + .2);0.30000000000000004
JavaSystem.out.println(.1 + .2);AndSystem.out.println(.1F + .2F);0.30000000000000004And0.3
Julia.1 + .20.30000000000000004
Julia has built-in rational numbers support and also a built-in arbitrary-precision BigFloat data type. To get the math right, 1//10 + 2//10 returns 3//10.
Clojure(+ 0.1 0.2)0.30000000000000004
Clojure supports arbitrary precision and ratios. (+ 0.1M 0.2M) returns 0.3M, while (+ 1/10 2/10) returns 3/10.
C#Console.WriteLine("{0:R}", .1 + .2);0.30000000000000004
GHC (Haskell)0.1 + 0.20.30000000000000004
Haskell supports rational numbers. To get the math right, (1 % 10) + (2 % 10) returns 3 % 10.
Hugs (Haskell)0.1 + 0.20.3
bc0.1 + 0.20.3
Nimecho(0.1 + 0.2)0.3
Gforth0.1e 0.2e f+ f.0.3
dc0.1 0.2 + p.3
Racket (PLT Scheme)(+ .1 .2)And(+ 1/10 2/10)0.30000000000000004And3/10
Rustextern crate num; use num::rational::Ratio; fn main() { println!(.1+.2); println!("1/10 + 2/10 = {}", Ratio::new(1, 10) + Ratio::new(2, 10)); }0.300000000000000043/10
Rust has rational number support from the num crate.
Emacs Lisp(+ .1 .2)0.30000000000000004
Turbo Pascal 7.0writeln(0.1 + 0.2);3.0000000000E-01
Common Lisp* (+ .1 .2)And* (+ 1/10 2/10)0.3And3/10
Gopackage main import "fmt" func main() { fmt.Println(.1 + .2) var a float64 = .1 var b float64 = .2 fmt.Println(a + b) fmt.Printf("%.54f/n", .1 + .2) }0.30.300000000000000040.299999999999999988897769753748434595763683319091796875
Go numeric constants have arbitrary precision.
Objective-C0.1 + 0.2;0.300000012
OCaml0.1 +. 0.2;;float = 0.300000000000000044
PowershellPS C:/>0.1 + 0.20.3
Prolog (SWI-Prolog)?- X is 0.1 + 0.2.X = 0.30000000000000004.
Perl 5perl -E 'say 0.1+0.2'perl -e 'printf q{%.17f}, 0.1+0.2'0.30.30000000000000004
Perl 6perl6 -e 'say 0.1+0.2'perl6 -e 'say sprintf(q{%.17f}, 0.1+0.2)'perl6 -e 'say 1/10+2/10'0.30.300000000000000000.3
Perl 6, unlike Perl 5, uses rationals by default, so .1 is stored something like { numerator => 1, denominator => 10 }..
Rprint(.1+.2)print(.1+.2, digits=18)0.30.300000000000000044
scalascala -e 'println(0.1 + 0.2)'Andscala -e 'println(0.1F + 0.2F)' Andscala -e 'println(BigDecimal("0.1") + BigDecimal("0.2"))'0.30000000000000004And0.3And0.3
Smalltalk0.1 + 0.2.0.30000000000000004
Swift0.1 + 0.2NSString(format: "%.17f", 0.1 + 0.2)0.30.30000000000000004
Dimport std.stdio; void main(string[] args) { writefln("%.17f", .1+.2); writefln("%.17f", .1f+.2f); writefln("%.17f", .1L+.2L); }0.299999999999999990.300000011920928960.30000000000000000
ABAPWRITE / CONV f( '.1' + '.2' ).AndWRITE / CONV decfloat16( '.1' + '.2' ).3.0000000000000004E-01And0.3

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 恩平市| 竹山县| 遂溪县| 木兰县| 托里县| 麦盖提县| 政和县| 法库县| 名山县| 清流县| 老河口市| 黎城县| 南昌县| 鄂温| 文山县| 日土县| 甘洛县| 长葛市| 鄂伦春自治旗| 兰州市| 横峰县| 永丰县| 普兰县| 通榆县| 雷山县| 新郑市| 闽清县| 开平市| 崇阳县| 垫江县| 鄯善县| 中西区| 西城区| 离岛区| 临城县| 特克斯县| 巴南区| 衡水市| 清丰县| 清丰县| 钦州市|