犀牛书第七版学习笔记:数据类型与结构-数字

JavaScript is a multi-paradigm, dynamic language多范式的动态语言 with types and operators类型和运算符, standard built-in objects标准的内置对象, and methods方法.

Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. 它的语法来源于 Java 和 C,所以这两种语言的许多语法特性同样适用于 JavaScript。

JavaScript supports object-oriented programming with object prototypes, instead of classes (see more about prototypical inheritance and ES2015 classes). JavaScript 通过原型链而不是类来支持面向对象编程

JavaScript also supports functional programming — because they are objects, functions may be stored in variables and passed around like any other object. JavaScript同样支持函数式编程——因为它们也是对象,函数也可以被保存在变量中,并且像其他对象一样被传递

0.概览

计算机程序通过操作数值来工作Computer programs work by manipulating values

在编程语言中可以表示和操作的值的种类被称为类型The kinds of values that can be represented and manipulated in a programming language are known as types,

编程语言最基本的特征之一是它支持的类型集and one of the most fundamental characteristics of a programming language is the set of types it supports.

当程序需要保留一个值以备将来使用时,它会将该值赋给(或“存储”在)一个变量中When a program needs to retain a value for future use, it assigns the value to (or “stores” the value in) a variable.

变量有名称,它们允许在程序中使用这些名称来引用值Variables have names, and they allow use of those names in our programs to refer to values.

变量的工作方式是任何编程语言的另一个基本特征The way that variables work is another fundamental characteristic of any programming language.

数字 

目录

0.概览

数字

1.不同进制的整数

1.16进制

2.8进制和2进制

2.浮点数字面量的指数表示法

1.一个看上去是整数的东西,其实都是浮点数

3.运算

1.内置对象 [Math]()

2.无穷大

3.正0和负0

4.NaN

5.ES6中新Number属性

1.内置函数 [parseInt()]()

2.内置函数 [parseFloat()]()

4.二进制浮点数和舍入错误Binary Floating-Point and Rounding Errors

5.使用BigInt的任意精确整数Arbitrary Precision Integers with BigInt

6.日期和时间Dates and Times


ECMAScript 标准定义了两种内建数值类型:Number(数字类型)和 BigInt

数字类型是一种基于 IEEE 754 标准的双精度 64 位二进制格式的值(从 -(2^53 -1) 到 2^53 - 1 之间的数字)。"double-precision 64-bit format IEEE 754 values"

当一个数字直接出现在JavaScript程序中时,它被称为数字字面量

When a number appears directly in a JavaScript program, it’s called anumeric literal

1.不同进制的整数

1.16进制

JavaScript可以识别十六进制(base-16)值。十六进制字面量以0x或0X开头

JavaScript recognizes hexadecimal (base-16) values. A hexadecimal literal begins with 0x or 0X

0xff // => 255: (15*16 + 15) 
0xBADCAFE // => 195939070

2.8进制和2进制

还可以使用前缀0b和0o(或0B和0O)而不是0x来表示二进制(以2为基数)或八进制(以8为基数)的整数

you can also express integers in binary (base 2) or octal (base 8) using the prefixes 0b and 0o (or 0B and 0O) instead of 0x

0b10101 // => 21: (1*16 + 0*8 + 1*4 + 0*2 + 1*1) 
0o377 // => 255: (3*64 + 7*8 + 7*1)

2.浮点数字面量的指数表示法

浮点字面量可以有一个小数点;他们使用传统的实数语法。实数表示为数字的整数部分,其后是小数点和小数部分。

浮点字面值也可以用指数表示法表示:一个实数后面跟着字母e(或E),后面跟着一个可选的加号或减号,再跟着一个整数指数。这个符号表示实数乘以10的指数次方

Floating-point literals may also be represented using exponential notation: a real number followed by the letter e (or E), followed by an optional plus or minus sign, followed by an integer exponent. This notation represents the real number multiplied by 10 to the power of the exponent

[digits][.digits][(E|e)[(+|-)]digits]

3.14 
2345.6789 
.333333333333333333 
6.02e23 // 6.02 × 10²³ 
1.4738223E-32 // 1.4738223 × 10⁻³²

1.一个看上去是整数的东西,其实都是浮点数

an apparent integer is in fact implicitly a float

console.log(3 / 2);             // 1.5, not 1
console.log(Math.floor(3 / 2)); // 1

0.1 + 0.2 == 0.30000000000000004;

3.运算

JavaScript 支持标准的算术运算符,包括加法、减法、取模(或取余)等等。

1.内置对象 [Math](<https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math>)

(数学对象),用以处理更多的高级数学函数和常数

Math.sin(3.5);
const circumference = 2 * Math.PI * r;

Math.pow(2,53) // => 9007199254740992: 2 to the power 53 
Math.round(.6) // => 1.0: round to the nearest integer 
Math.ceil(.6) // => 1.0: round up to an integer 
Math.floor(.6) // => 0.0: round down to an integer 
Math.abs(-5) // => 5: absolute value 
Math.max(x,y,z) // Return the largest argument 
Math.min(x,y,z) // Return the smallest argument 
Math.random() // Pseudo-random number x where 0 <= x < 1.0 
Math.PI // π: circumference of a circle / diameter 
Math.E // e: The base of the natural logarithm 
Math.sqrt(3) // => 3**0.5: the square root of 3 
Math.pow(3, 1/3) // => 3**(1/3): the cube root of 3 
Math.sin(0) // Trigonometry: also Math.cos, Math.atan, etc. 
Math.log(10) // Natural logarithm of 10 
Math.log(100)/Math.LN10 // Base 10 logarithm of 100 
Math.log(512)/Math.LN2 // Base 2 logarithm of 512 
Math.exp(3) // Math.E cubed

JavaScript中的算术在溢出overflow、下溢underflow或除零division by zero的情况下不会引发错误

2.无穷大

当数值运算的结果大于最大的可表示数字(溢出)时,结果是一个特殊的无穷大值infinity。

当一个负数的绝对值大于最大可表示负数的绝对值时,结果是负无穷

对无限大值进行加、减、乘或除,结果会是无限大值(可能符号颠倒了)。

1 / 0; //  Infinity
-1 / 0; // -Infinity

//可以使用内置函数 isFinite() 来判断一个变量是否是一个有穷数, 如果类型为Infinity, -Infinity 或 NaN则返回false
isFinite(1/0); // false
isFinite(Infinity); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

isFinite(0); // true
isFinite(2e64); // true

isFinite("0"); // true
// 如果是纯数值类型的检测,则返回 false:
Number.isFinite("0"); // false

3.正0和负0

当数值运算的结果比最小的可表示数字更接近于零时,就会发生下溢。在这种情况下,JavaScript返回0。

如果下溢发生在负数,JavaScript将返回一个特殊的值,称为“负零”。这个值与普通的0几乎没有区别。

正0和负0几乎是无法区分的

let zero = 0; // Regular zero 
let negz = -0; // Negative zero 
zero === negz // => true: zero and negative zero are equal 
1/zero === 1/negz // => false: Infinity and -Infinity are not equal

4.NaN

在JavaScript中,除以零并不是一个错误:它只是返回无穷大或负无穷大。

0除以0操作的结果是特殊的非数字值NaN。

无穷大除以无穷大、对负数开平方根或使用不能转换为数字的非数字操作数的算术运算符,也会出现NaN。

JavaScript预定义了全局常量Infinity和NaN来保存正的无穷大值和非数字值,这些值也可以作为Number对象的属性使用

JavaScript predefines global constants Infinity and NaN to hold the positive infinity and not-a-number value, and these values are also available as properties of the Number object

Infinity // A positive number too big to represent 
Number.POSITIVE_INFINITY // Same value 
1/0 // => Infinity 
Number.MAX_VALUE * 2 // => Infinity; overflow 

-Infinity // A negative number too big to represent 
Number.NEGATIVE_INFINITY // The same value 
-1/0 // => -Infinity 
-Number.MAX_VALUE * 2 // => -Infinity 

NaN // The not-a-number value 
Number.NaN // The same value, written another way 
0/0 // => NaN 
Infinity/Infinity // => NaN

Number.MIN_VALUE/2 // => 0: underflow 
-Number.MIN_VALUE/2 // => -0: negative zero 
-1/Infinity // -> -0: also negative 0 
-0

not-a-number值在JavaScript中有一个不寻常的特性:它不等于任何其他值,包括它自己。

这意味着不能编写x === NaN来确定变量x的值是否为NaN。

必须写x != xNumber.isNaN(x)。当且仅当x与全局常量NaN具有相同的值时,这些表达式将为真

全局函数isNaN()类似于Number.isNaN()。如果它的参数是NaN,或者该参数是不能转换为数字的非数字值,则返回true。

Number . isfinite()如果其参数不是NaN、Infinity或-Infinity,则返回true。

全局函数isFinite()的参数如果是有限数,或者可以转换为有限数,则返回true

//更直接和自然,更可靠
Number.isNaN(NaN); // true
Number.isNaN('hello'); // false
Number.isNaN('1'); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN([1]) // false
Number.isNaN([1,2]) // false

//反直觉的 **考虑到了参数可能是无法转化为数字的值**
isNaN('hello'); // true
isNaN('1'); // false
isNaN(undefined); // true
isNaN({}); // true
isNaN([1]) // false
isNaN([1,2]) // true

5.ES6中新Number属性

Number.parseInt() // Same as the global parseInt() function 
Number.parseFloat() // Same as the global parseFloat() function 
Number.isNaN(x) // Is x the NaN value? 
Number.isFinite(x) // Is x a number and finite? 
Number.isInteger(x) // Is x an integer? 
Number.isSafeInteger(x) // Is x an integer -(2**53) < x < 2**53? 
Number.MIN_SAFE_INTEGER // => -(2**53 - 1) 
Number.MAX_SAFE_INTEGER // => 2**53 - 1 
Number.EPSILON // => 2**-52: smallest difference between numbers

1.内置函数parseInt()

将字符串转换为interger

第二个参数:进制 the base for the conversion

//十进制
parseInt('123', 10); // 123
parseInt('010', 10); // 10

parseInt('010');  //  8 0开头的字符串视为8进制
parseInt('0x10'); // 16 0x开头的字符串视为16进制

parseInt('hello', 10); // NaN ((Not a Number 的缩写)) 给定的字符串不存在数值形式
NaN + 5; // NaN NaN 作为参数进行任何数学运算,结果也会是 NaN

isNaN(NaN); // true 可以使用内置函数 isNaN() 来判断一个变量是否为 NaN

2.内置函数parseFloat()

解析浮点数字符串,只应用于解析十进制数字

4.二进制浮点数和舍入错误Binary Floating-Point and Rounding Errors

实数有无限多,但只有有限的数(准确地说,是18,437,736,874,454,810,627)可以用JavaScript浮点格式精确地表示。这意味着在JavaScript中处理实数时,数字的表示通常是实际数字的近似值

JavaScript(以及几乎所有其他现代编程语言)使用的IEEE-754浮点表示是一种二进制表示,它可以精确地表示像1/2、1/8和1/1024这样的分数。不幸的是,我们最常用的分数(尤其是在执行金融计算时)是十进制分数:1/10、1/100等等。

二进制浮点表示法不能精确地表示0.1这样简单的数字。只能做到非常接近0.1。这个数字不能准确表示的事实可能会导致问题。

let x = .3 - .2; // thirty cents minus 20 cents 
let y = .2 - .1; // twenty cents minus 10 cents 
x === y // => false: the two values are not the same! 
x === .1 // => false: .3-.2 is not equal to .1 
y === .1 // => true: .2-.1 is equal to .1

由于舍入误差(rounding error),.3和.2的近似值之间的差异与.2和.1的近似值之间的差异并不完全相同。

这个问题不是JavaScript特有的:它影响任何使用二进制浮点数的编程语言

这里显示的代码中的x和y值非常接近,也非常接近正确的值。计算出的数值几乎适用于任何用途;只有当我们试图比较相等的值时,问题才会出现。

5.使用BigInt的任意精确整数Arbitrary Precision Integers with BigInt

BigInt作为新数字类型是ES2020的新特性。

BigInt是一种数值为整数的数字类型。将该类型添加到JavaScript主要是为了允许表示64位整数64-bit integers,这是与许多其他编程语言和api兼容所必需的。

如果需要处理这么大的数字,BigInt值可以有数千位甚至数百万位thousands or even millions of digits数字。

BigInt字面值被写为后跟小写字母n的一串数字。

默认情况下,它们以10为基数,但对于二进制、八进制和十六进制的BigInt,可以使用0b、0o和0x前缀

1234n // A not-so-big BigInt literal 
0b111111n // A binary BigInt 
0o7777n // An octal BigInt 
0x8000000000000000n // => 2n**63n: A 64-bit integer

可以使用BigInt()函数将常规JavaScript数字或字符串转换为BigInt值

BigInt(Number.MAX_SAFE_INTEGER) // => 9007199254740991n 
let string = "1" + "0".repeat(100); // 1 followed by 100 zeros. 
BigInt(string) // => 10n**100n: one googol

使用BigInt值的算术就像使用常规JavaScript数字的算术一样,只是除法去掉了所有余数并向下舍入(接近于0)

1000n + 2000n // => 3000n 
3000n - 2000n // => 1000n 
2000n * 3000n // => 6000000n 
3000n / 997n // => 3n: the quotient商 is 3 
3000n % 997n // => 9n: and the remainder is 9 
(2n ** 131071n) - 1n // A Mersenne prime with 39457 decimal digits一个有39457个十进制数字的梅森素数

6.日期和时间Dates and Times

JavaScript定义了一个简单的Date类,用于表示和操作表示日期和时间的数字。

JavaScript Dates是对象,但它们也有一个时间戳的数字表示形式,用于指定自1970年1月1日以来所经过的毫秒数

let timestamp = Date.now(); // The current time as a timestamp (a number). 
let now = new Date(); // The current time as a Date object. 
let ms = now.getTime(); // Convert to a millisecond timestamp. 
let iso = now.toISOString(); // Convert to a string in standard format.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值