LoginSignup
12
4

More than 5 years have passed since last update.

色々な言語でとにかく0.0で割ってみた

Last updated at Posted at 2018-08-21

概要

複数の言語で、数値を0.0(浮動小数点な数値と類されるもの)で割ってみます。

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

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

JavaScript(Node)

$ node -v
v7.7.1
$ node -e 'console.log(1.0/0.0)'
Infinity

Ruby

$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
$ ruby -e 'puts(1.0/0.0)'
Infinity

Python(CPython)

$ python --version
Python 2.7.10
$ python -c 'print(1.0/0.0)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ZeroDivisionError: float division by zero

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(1.0/0.0)'
Illegal division by zero at -e line 1.

PowerShell Core

$ pwsh -v
PowerShell v6.0.4
$ pwsh -C 'echo (1.0/0.0)'
∞

(∞っていいですね。)

Scheme(Gauche)

$ gosh -V
Gauche scheme shell, version 0.9.6 [utf-8,pthreads], x86_64-apple-darwin17.7.0
$ gosh -e '(print (/ 1.0 0.0))'
+inf.0

awk

$ awk -version
awk version 20070501
$ awk 'BEGIN{print(1.0/0.0)}'
awk: division by zero
 source line number 1

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(1.0/0.0)'
Inf

zsh

$ zsh --version
zsh 5.3 (x86_64-apple-darwin17.0)
$ zsh -c 'echo $((1.0/0.0))'
zsh:1: division by zero

emacs-lisp

(9/1 chuntaroさんよりいただいた内容で更新)

$ emacs --version
GNU Emacs 22.1.1
$ emacs --batch --eval '(print (/ 1.0 0.0))'

1.0e+INF

もとの記載がこちらです。
$ emacs --version
GNU Emacs 22.1.1
Copyright (C) 2007 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
$ cat div.el 
(message "ans = %f" (/ 1.0 0.0))
$ emacs --batch div.el -f eval-buffer
Loading subst-ksc...
Loading subst-gb2312...
Loading subst-big5...
Loading subst-jis...
ans = inf

lua

$ lua -v
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
$ lua -e 'print(1.0/0.0)'
inf

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 '1.0/0.0'
> 1.0/0.0
[1] Inf

1ライナー以外

Swift

$ swift
Welcome to Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2). Type :help for assistance.
  1> print(1.0/0.0)
inf

Java

$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
$ cat ZeroDevide.java 

public class ZeroDevide {
    public static void main(String[] args) {
        System.out.println("" + (1.0/0.0));
    }
}
$ java ZeroDevide
Infinity

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 zero_devide.c 
#include <stdio.h>

int main() {
    printf("%f\n", 1.0/0.0);
}
$ cc zero_devide.c 
$ ./a.out 
inf

C# (on Mono)

$ mcs --version
Mono C# compiler version 4.8.0.0
$ cat divzero.cs
using System;
class Hello 
{
    static void Main() 
    {
        System.Console.WriteLine("" + (1.0/0.0));
    }
}
$ mcs divzero.cs 
$ mono divzero.exe 
Infinity

Go

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

import (
    "fmt"
    "strconv"
)

func main() {
    zero := 1.0 - 1.0 
    s := strconv.FormatFloat(1.0/zero, 'F', -1, 64)
    fmt.Print(s)
}

$ go build zero_devide.go 
$ ./zero_devide 
+Inf

なお、リテラルで書くとこうなります。

$ cat zero_devide.go
package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := strconv.FormatFloat(1.0/0.0, 'F', -1, 64)
    fmt.Print(s)
}
$ go build zero_devide.go 
# command-line-arguments
./zero_devide.go:9:30: division by zero

Haskell

$ ghc -V
The Glorious Glasgow Haskell Compilation System, version 8.4.3
$ cat divzero.hs 
a = 1.0 / 0.0
main = do
  print a
$ runghc divzero.hs 
Infinity

言語以外

Tensorflow

$ python -c 'import tensorflow as tf; print(tf.__version__)'
(snip)
1.9.0
import tensorflow as tf

x = tf.constant(1.0, name='x')
y = tf.Variable(x / 0.0, name='y')

model = tf.initialize_all_variables()

with tf.Session() as session:
    session.run(model)
    print(session.run(y))

$ python divzero.py 
(snip)
inf

Numbers

スクリーンショット 2018-08-26 17.24.44.png

スクリーンショット 2018-08-26 17.24.53.png

所感

IEEE754で割り算するにも表現方法がいくつかあるし、ゼロ除算になる系もあるのだなと思いました。
特定のツールでものを作る時には、ルールブックとして、心に留めておくといいことがあるかもしれないですね。

12
4
2

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
12
4