- 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.
推薦閱讀
- 從零構建知識圖譜:技術、方法與案例
- Mastering ServiceStack
- Visual FoxPro程序設計教程
- Mastering Articulate Storyline
- HTML5 Mobile Development Cookbook
- C語言程序設計
- 零基礎學Java(第4版)
- H5頁面設計:Mugeda版(微課版)
- Python極簡講義:一本書入門數據分析與機器學習
- Python Interviews
- ExtJS Web應用程序開發指南第2版
- C++ Application Development with Code:Blocks
- Nagios Core Administration Cookbook(Second Edition)
- 單片機原理及應用技術
- 深入分析GCC