7
1

More than 5 years have passed since last update.

色々な言語でとにかく0.1+0.2をやってみた

Posted at

概要

複数の言語で、数値を0.1と0.2を(浮動小数点な数値と類されるもの)足してみます。
計算誤差のことを何も考えずに足し算をして、一般的と考えられるコンソール表示用の関数(print系のもの)で表示させた時の様子を観察してみることが目的です。
数値計算の際には実際は専用のライブラリ・関数群を使いますが、それらを使わずにナイーブに数値を書くとどうなるかしら、ということを観察しています。

試した環境は、macOS 10.13.6です。

1ライナーでためせるもの

JavaScript(Node)

$ node -v
v7.7.1
$ node -e 'console.log(0.1+0.2)'
0.30000000000000004

Ruby

$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
$ ruby -e 'puts(0.1+0.2)'
0.30000000000000004

Python(CPython)

$ python --version
Python 2.7.10
$ python -c 'print(0.1+0.2)'
0.3
$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1+0.2
0.30000000000000004

Perl(Perl5)

$ perl -version | grep 'This'
This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
$ perl -e 'print(0.1+0.2)'
0.3

PowerShell Core

$ pwsh -v
PowerShell v6.0.4
$ pwsh -C 'echo (0.1+0.2)'
0.3

Scheme(Gauche)

$ gosh -V
Gauche scheme shell, version 0.9.6 [utf-8,pthreads], x86_64-apple-darwin17.7.0
$ gosh -e '(print (+ 0.1 0.2))'
0.30000000000000004

awk

$ awk -version
awk version 20070501
$ awk 'BEGIN{print(0.1+0.2)}'
0.3

Julia

$ /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia -v
julia version 1.0.0
$ /Applications/Julia-1.0.app/Contents/Resources/julia/bin/julia -e 'println(0.1+0.2)'
0.30000000000000004

zsh

$ zsh --version
zsh 5.3 (x86_64-apple-darwin17.0)
$ zsh -c 'echo $((0.1+0.2))'
0.30000000000000004

emacs-lisp

$ emacs --version
GNU Emacs 22.1.1
$ emacs --batch --eval '(print (+ 0.1 0.2))'

0.30000000000000004

lua

$ lua -v
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
$ lua -e 'print(0.1+0.2)'
0.3

R

$ /Library/Frameworks/R.framework/Resources/bin/R --version | grep "R version"
R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
$ $ /Library/Frameworks/R.framework/Resources/bin/R -q -e '0.1+0.2'
> 0.1+0.2
[1] 0.3

1ライナー以外

Swift

$ swift 
Welcome to Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1). Type :help for assistance.
  1> print(0.1+0.2)
0.30000000000000004

Java

$ java -version
openjdk version "1.8.0_202"
OpenJDK Runtime Environment Corretto-8.202.08.2 (build 1.8.0_202-b08)
OpenJDK 64-Bit Server VM Corretto-8.202.08.2 (build 25.202-b08, mixed mode)
$ cat FloatingPointError.java 

public class FloatingPointError {
    public static void main(String[] args) {
        System.out.println("" + (0.1+0.2));
    }
}
$ java FloatingPointError
0.30000000000000004

C (Clang, Apple LLVM)

$ cc -v
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ cat floating_point_error.c
#include <stdio.h>

int main() {
    printf("%f\n", 0.1 + 0.2);
}
$ cc floating_point_error.c
$ ./a.out 
0.300000

C# (on Mono)

$ mcs --version
Mono C# compiler version 4.8.0.0
$ cat floaterror.cs 
using System;
class FloatError
{
    static void Main() 
    {
        System.Console.WriteLine("" + (0.1+0.2));
    }
}
$ mcs floaterror.cs 
$ mono floaterror.exe 
0.3

Go

$ go version
go version go1.10.3 darwin/amd64
$ cat float_error.go 
package main

import (
    "fmt"
)

func main() {
    fmt.Print(0.1 + 0.2)
}
$ go build float_error.go
$ ./float_error 
0.3

Haskell

$ ghc -V
The Glorious Glasgow Haskell Compilation System, version 8.4.3
$ cat floaterror.hs 
a = 0.1 + 0.2
main = do
  print a
$ runghc floaterror.hs 
0.30000000000000004

Numbers

スクリーンショット 2019-05-05 13.03.25.png

デフォルトのセルの設定では下記の通りです。

スクリーンショット 2019-05-05 12.56.11.png

スクリーンショット 2019-05-05 12.56.18.png

桁数を増やしてみると

スクリーンショット 2019-05-05 13.02.03.png

スクリーンショット 2019-05-05 13.02.34.png

関連

このエントリは、次のエントリと関連があります。

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1