Apr 17, 2023
What should you learn in this course
Random Number Generator
+ : addition
- : subtraction
/ : division
* : multiplication
% : modulus
- The sign bit (the 31st bit): positive is 0, negative is 1.
- The nex 8 bits: the exponent value.
- The remaining 23 bits represent the fraction value.
This results in the following:
- 자바스크립트의 숫자 시스템은 다른 언어와 달리 32-bit floating-point representation을 사용하고 있다.
- 기본적으로 숫자는 컴퓨터에 저장될 때 2진수로 저장된다. 그러나, 숫자 10이 2진수로 저장될 때 1001로 저장되는 것과 달리, 소수점은 깔끔하게 떨어지지 않는다. 따라서 아래와 같은 결과가 나온다.
To really understand why 0.1 cannot be represented properly as a 32-bit floating- point number, you must understand binary. Representing many decimals in binary requires an infinite number of digits. This because binary numbers are represented by where n is an integer.
What is integer division?
3/2 = 1, 항상 정수만 리턴하는 나눗셈. round down 값을 반환
Integer division in programming languages like Java simply evaluates division expressions to their quotient (몫).
5/4 is 1 in Java because the quotient is
5/4 = 1.25
This is because Java requires you to explicitly (명시적으로, 확실하게) type the integer as an integer.
Math.round - rounds to nearest integer
Math.floor - rounds down to nearest integer
Math.ceil - rounds up to nearest integer
Number.EPSILON returns the smallest interval between two representable numbers. This is useful for the problem with floating-point approximation.
This function works by checking whether the difference between the two numbers are smaller than
Remember that Number.EPSILON is the smallest difference between two representable numbers. The difference between 0.1+0.2 and 0.3 will be smaller than Number.EPSILON.
⇒ For example, the smallest number among the bigger number in 0.1 will be 0.00000000001…. something. However, computer can recognize that small number so they decide epsilon which is the smallest number of JS to compare the size of numbers. Therefore, the smallest number among the bigger number in 0.1 will be
0.1 + EPSILON
It returns the largest integer.
what is integer?
정수. 정수는 양의정수, 음의정수, 그리고 0이 있다. (분수, 소수 등 포함 x)
- The value of the largest integer n such that n and n + 1 are both exactly representable as a Number value. Therefore, it does not work for floating-point decimals.
It returns the largest floating-point number possible. Equal to approximately 1.79E+308.
Therefore, this uses double-precision floating-point representation and works for floating points as well.
It returns the smallest integer. It is equal to -9007199254740991.
Number.MIN_VALUE returns the smallest floating-point number possible.
Number.MIN_VALUE is equal to 5e-324. This is not a negative number since it is the smallest floating-point number possible and means that Number.MIN_VALUE is actually bigger than Number.MIN_- SAFE_INTEGER.
Number.MIN_VALUE is also the closest floating point to zero.
The only thing greater than Number.MAX_VALUE is Infinity, and the only thing smaller than Number.MAX_SAFE_INTEGER is -Infinity.
Infinity < Number.MIN_SAFE_INTEGER < Number.MIN_VALUE < 0 < Number.MAX_
SAFE_IN- TEGER < Number.MAX_VALUE < Infinity
A primality test can be done by iterating from 2 to n, checking whether modulus division (remainder) is equal to zero.
What is prime number?
The time complexity is O(n) because this algorithm checks all numbers from 0 to n.
- Think about how this method iterates through 2 to n. Is it possible to find a pattern and make the algorithm faster? First, any multiple of 2s can be ignored, but there is more optimization possible. This is difficult to notice, but all primes are of the form
6k ± 1, with the exception of 2 and 3 where k is some integer. Here’s an example:
- Also realize that for testing the prime number n, the loop only has to test until the
square root of
n. This is because if the square root of n is not a prime number, n is not a prime number by mathematical definition.
This improved solution cuts the time complexity down significantly.
Prime numbers are the basis of encryption (covered in Chapter 4) and hashing (covered in Chapter 11), and prime factorization is the process of determining which prime numbers multiply to a given number. Given 10, it would print 5 and 2.
This algorithm works by printing any number that is divisible by i without a remainder. In the case that a prime number is passed into this function, it would be handled by printing whether n is greater than 2.
It returns a float between 0 and 1.
Simply multiply Math.random() by the range. Add or subtract from it to set the base.
Simply use Math.floor(), Math.round(), or Math.ceil() to round to an integer.