effective javascript(一)——认识javascript浮点数

一、javascript中所有数字的类型都为number类型

typeof 10: number
typeof 15.8: number
typeof -30.2: number

二、javascript中所有数字都是双精度浮点数,大多数的算术运算可以使用整数、实数或者两者进行计算

0.5 * 1.7 = 0.85
-20 + 35 = 15
18 - 3.65 = 14.35
2.8 / 4 = 0.7
29 % 5 = 4

三、位运算采用特殊的计算方式,javascript将操作数隐式转换为32位二进制后进行运算

5 | 8 = 13
(5).toString(2) = 101
(8).toString(2) = 1000
parseInt("1101",2) = 13

四、浮点数计算精度问题(常见问题)

0.2 + 0.4 = 0.6000000000000001
0.5 + 0.4 = 0.9
(0.2 + 0.4) + 0.3 = 0.9000000000000001
0.2 + (0.4 + 0.3) = 0.8999999999999999

解决方法之一:计算结果使用toFixed方法保留一定的小数位数。

如:(0.2 + 0.4).toFixed(1)= 0.6
☆ 资源说明:☆ [Addison-Wesley Professional] Effective JavaScript 编写高质量JavaScript代码的68个有效方法 (英文版) [Addison-Wesley Professional] Effective JavaScript 68 Specific Ways to Harness the Power of JavaScript (E-Book) ☆ 图书概要:☆ In order to truly master JavaScript, you need to learn how to work effectively with the language’s flexible, expressive features and how to avoid its pitfalls. No matter how long you’ve been writing JavaScript code, Effective JavaScript will help deepen your understanding of this powerful language, so you can build more predictable, reliable, and maintainable programs. Author David Herman, with his years of experience on Ecma’s JavaScript standardization committee, illuminates the language’s inner workings as never before—helping you take full advantage of JavaScript’s expressiveness. Reflecting the latest versions of the JavaScript standard, the book offers well-proven techniques and best practices you’ll rely on for years to come. Effective JavaScript is organized around 68 proven approaches for writing better JavaScript, backed by concrete examples. You’ll learn how to choose the right programming style for each project, manage unanticipated problems, and work more successfully with every facet of JavaScript programming from data structures to concurrency. ☆ 出版信息:☆ [作者信息] David Herman [出版机构] Addison-Wesley Professional [出版日期] 2012年12月06日 [图书页数] 240页 [图书语言] 英语 [图书格式] PDF 格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值