1. 程式人生 > >0.1 + 0.2 = 0.30000000000000004

0.1 + 0.2 = 0.30000000000000004

今天才知道原來 0.1 + 0.2 不是 0.3

相關網站:

Language Code Result
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
PHP echo .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).
MySQL SELECT .1 + .2; 0.3
Postgres SELECT select 0.1::float + 0.2::float; 0.3
Delphi XE5 writeln(0.1 + 0.2); 3.00000000000000E-0001
Erlang io:format("~w~n", [0.1 + 0.2]).
0.30000000000000004
Elixir IO.puts(0.1 + 0.2) 0.30000000000000004
Ruby puts 0.1 + 0.2
And
puts 1/10r + 2/10r
0.30000000000000004
And
3/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 2 print(.1 + .2)
And
float(decimal.Decimal(".1") + decimal.Decimal(".2"))
And
.1 + .2
0.3
And
0.3
And
0.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 3 print(.1 + .2)
And
.1 + .2
0.30000000000000004
And
0.30000000000000004
Lua print(.1 + .2)
print(string.format("%0.17f", 0.1 + 0.2))
0.3
0.30000000000000004
JavaScript document.writeln(.1 + .2); 0.30000000000000004
Java System.out.println(.1 + .2);
And
System.out.println(.1F + .2F);
0.30000000000000004
And
0.3
Julia .1 + .2 0.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.2 0.30000000000000004
Haskell supports rational numbers. To get the math right, (1 % 10) + (2 % 10) returns 3 % 10.
Hugs (Haskell) 0.1 + 0.2 0.3
bc 0.1 + 0.2 0.3
Nim echo(0.1 + 0.2) 0.3
Gforth 0.1e 0.2e f+ f. 0.3
dc 0.1 0.2 + p .3
Racket (PLT Scheme) (+ .1 .2)
And
(+ 1/10 2/10)
0.30000000000000004
And
3/10
Rust
extern 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.30000000000000004
3/10
Emacs Lisp (+ .1 .2) 0.30000000000000004
Turbo Pascal 7.0 writeln(0.1 + 0.2); 3.0000000000E-01
Common Lisp * (+ .1 .2)
And
* (+ 1/10 2/10)
0.3
And
3/10
Go
package 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.3
0.30000000000000004
0.299999999999999988897769753748434595763683319091796875
Objective-C 0.1 + 0.2; 0.300000012
OCaml 0.1 +. 0.2;; float = 0.300000000000000044
Powershell PS C:\>0.1 + 0.2 0.3
Prolog (SWI-Prolog) ?- X is 0.1 + 0.2. X = 0.30000000000000004.
Perl 5 perl -E 'say 0.1+0.2'
perl -e 'printf q{%.17f}, 0.1+0.2'
0.3
0.30000000000000004
Perl 6 perl6 -e 'say 0.1+0.2'
perl6 -e 'say sprintf(q{%.17f}, 0.1+0.2)'
perl6 -e 'say 1/10+2/10'
0.3
0.30000000000000000
0.3
R print(.1+.2)
print(.1+.2, digits=18)
0.3
0.300000000000000044
scala scala -e 'println(0.1 + 0.2)'
And
scala -e 'println(0.1F + 0.2F)' And
scala -e 'println(BigDecimal("0.1") + BigDecimal("0.2"))'
0.30000000000000004
And
0.3
And
0.3
Smalltalk 0.1 + 0.2. 0.30000000000000004
Swift 0.1 + 0.2
NSString(format: "%.17f", 0.1 + 0.2)
0.3
0.30000000000000004
D
import std.stdio;

void main(string[] args) {
    writefln("%.17f", .1+.2);
    writefln("%.17f", .1f+.2f);
    writefln("%.17f", .1L+.2L);
}
0.29999999999999999
0.30000001192092896
0.30000000000000000
ABAP WRITE / CONV f( '.1' + '.2' ).
And
WRITE / CONV decfloat16( '.1' + '.2' ).
3.0000000000000004E-01
And
0.3