私のPerlに対する知識が節穴だということを再認識しました。
「3 / 2」の結果を「1」になると思っていた。答えは「1.5」でした。
で、気になったので「3 / 2」が「1」になる子と「1.5」になる子を調べてみた。
================================
1.5の子たち
================================
■JavaScript => 1.5
<script type="text/javascript">
alert(3/2);
</script>
■Perl => 1.5
print 3 / 2;
■VBScript => 1.5
WScript.Echo 3 / 2
■VisualBasic => 1.5
MsgBox(3 / 2)
■ActionScript => 1.5
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
<mx:Button id="id1" label="button" click="Alert.show(String(3 / 2))"/>
</mx:Application>
================================
1の子たち
================================
■Ruby => 1
p 3 / 2
■Python => 1
print 3 / 2
※20090502追記:python3系は1.5
■Java => 1
public class SanWaruNi {
public static void main(String[] args) {
System.out.println(3 / 2);
}
}
■C# => 1
MessageBox.Show((3 / 2).ToString());
■C => 1
#include <stdio.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", 3 / 2);
return 0;
}
■C++ => 1
#include <iostream>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << 3 / 2;
return 0;
}
■コマンドプロンプト => 1
SET /A 3 / 2
■bash => 1
num=`expr 3 / 2`
うちのパソコンに入っている言語はここまで。
Perlはデフォルト浮動小数。他の「1」にしてる子達はどうなんだろう。ActionScriptは確か浮動小数点数のNumber型が使われていた気がする。VBはしっかり勉強したことがないので分からない……