官术网_书友最值得收藏!

Floats

In Elixir, floats are written with a decimal point, with digits before and after it, meaning that .1 is not a valid float in Elixir (as it is, for instance, in JavaScript). In Elixir, you have to be explicit and write the leading 0—so in this case, you'd write 0.1. Here's an example of the multiplication of two floats:

iex> 0.1 * 0.5
0.05

You can also write floats using the exponent notation, as shown:

iex> 0.1e3
100.0

Floats are represented in IEEE 754 double precision, which yields between 15 to 17 significant decimal digits. As usual, you should take care when comparing floats for equality.

Beware that the division operator ( /) always returns a float, even if the result of the division could be an integer:

iex> 4/2
2.0

If you want to circumvent this behavior, use the auto-imported div function from the Kernel module. Also, if you want to get the remainder of a division, use the rem function.

主站蜘蛛池模板: 邓州市| 双峰县| 石河子市| 安塞县| 江源县| 康平县| 柳林县| 古浪县| 临海市| 望江县| 罗定市| 金阳县| 名山县| 绵阳市| 且末县| 铁岭市| 富裕县| 宁国市| 连州市| 登封市| 东辽县| 镇坪县| 马山县| 拉萨市| 青阳县| 贡觉县| 汾阳市| 神农架林区| 绵竹市| 浦城县| 隆德县| 利辛县| 留坝县| 横峰县| 陵川县| 秦安县| 商城县| 原阳县| 莒南县| 息烽县| 乡城县|