- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 164字
- 2021-08-05 10:42:44
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.
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.
推薦閱讀
- 多媒體CAI課件設(shè)計(jì)與制作導(dǎo)論(第二版)
- VMware View Security Essentials
- Progressive Web Apps with React
- Android項(xiàng)目開發(fā)入門教程
- ASP.NET Core 5.0開發(fā)入門與實(shí)戰(zhàn)
- Instant Zepto.js
- 軟件測(cè)試工程師面試秘籍
- Practical DevOps
- Python數(shù)據(jù)分析(第2版)
- MATLAB定量決策五大類問(wèn)題
- 鋒利的SQL(第2版)
- Learning Hunk
- Advanced UFT 12 for Test Engineers Cookbook
- C#面向?qū)ο蟪绦蛟O(shè)計(jì)(第2版)
- Python一行流:像專家一樣寫代碼