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

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.

主站蜘蛛池模板: 梨树县| 曲阜市| 襄樊市| 安国市| 岱山县| 饶河县| 苍山县| 嘉定区| 墨竹工卡县| 五台县| 红河县| 云阳县| 诏安县| 滦南县| 延庆县| 新巴尔虎右旗| 富宁县| 黄山市| 天气| 宁武县| 闸北区| 江源县| 滕州市| 翁源县| 恩平市| 潍坊市| 沂南县| 喀喇| 宁陕县| 石家庄市| 榆树市| 墨玉县| 绥中县| 乌鲁木齐县| 紫云| 韶关市| 哈尔滨市| 千阳县| 西畴县| 新竹市| 忻州市|