Update page 'Day 1 homework - Xuanyi Wang (Sloetjes)'
@@ -3,24 +3,24 @@
|
|||||||
|
|
||||||
### 1. What is the name of the international standard for floating-point representation?
|
### 1. What is the name of the international standard for floating-point representation?
|
||||||
|
|
||||||
<span style="color:blue">**IEEE 754**</span>
|
**IEEE 754**
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 2. How many bits are the floating point numbers defined by that standard allowed to be?
|
### 2. How many bits are the floating point numbers defined by that standard allowed to be?
|
||||||
|
|
||||||
|
|
||||||
####<span style="color:blue"> Single-precision format (32-bits)</span>
|
####Single-precision format (32-bits)
|
||||||
|
|
||||||
Single-precision format floating-point numbers are represented using 32 bits, with 1 bit for the sign, 8 bits for the exponent, and 23 bits for the significand (mantissa);
|
Single-precision format floating-point numbers are represented using 32 bits, with 1 bit for the sign, 8 bits for the exponent, and 23 bits for the significand (mantissa);
|
||||||
|
|
||||||
####<span style="color:blue"> Double-precision format (64-bits)</span>
|
####Double-precision format (64-bits)
|
||||||
|
|
||||||
Double-precision floating-point numbers are represented using 64 bits, with 1 bit for the sign, 11 bits for the exponent, and 52 bits for the significand.
|
Double-precision floating-point numbers are represented using 64 bits, with 1 bit for the sign, 11 bits for the exponent, and 52 bits for the significand.
|
||||||
|
|
||||||
* The IEEE 754 standard also defines extended-precision and quadruple-precision formats, which use <span style="color:blue">80 bits</span> and <span style="color:blue">128 bits</span>, respectively.
|
* The IEEE 754 standard also defines extended-precision and quadruple-precision formats, which use 80 bitsand 128 bits, respectively.
|
||||||
|
|
||||||
* However, these formats are less commonly used than the single-precision and double-precision formats.</span>
|
* However, these formats are less commonly used than the single-precision and double-precision formats.
|
||||||
|
|
||||||
|
|
||||||
### 3. Does your computer support all those formats? Can you think of an easy way to check with only one line of Python?
|
### 3. Does your computer support all those formats? Can you think of an easy way to check with only one line of Python?
|
||||||
@@ -62,7 +62,7 @@ print(df)
|
|||||||
|
|
||||||
### 1. You can use sys.getsizeof to see how large a Python object is. Does the size of a Python floating-point number change as it gets larger? Why or why not?
|
### 1. You can use sys.getsizeof to see how large a Python object is. Does the size of a Python floating-point number change as it gets larger? Why or why not?
|
||||||
|
|
||||||
**<span style="color:blue"> The size of a Python floating-point number does not change as it gets larger.**
|
**The size of a Python floating-point number does not change as it gets larger.**
|
||||||
|
|
||||||
|
|
||||||
In Python, floating-point numbers are represented using the IEEE 754 standard, which uses a fixed number of bits to represent the number's value and precision. Specifically, Python uses 64 bits to represent a float.
|
In Python, floating-point numbers are represented using the IEEE 754 standard, which uses a fixed number of bits to represent the number's value and precision. Specifically, Python uses 64 bits to represent a float.
|
||||||
@@ -73,18 +73,18 @@ However, it's worth noting that as the magnitude of a floating-point number grow
|
|||||||
|
|
||||||
### 2. We already know that the precision of floating-point numbers decreases as their magnitude increases. Using Pandas, create a dataframe representing a large amount of US Dollars. What is the smallest number of dollars where adding one cent begins to yield wrong results?
|
### 2. We already know that the precision of floating-point numbers decreases as their magnitude increases. Using Pandas, create a dataframe representing a large amount of US Dollars. What is the smallest number of dollars where adding one cent begins to yield wrong results?
|
||||||
|
|
||||||
Assuming double-precision floating-point numbers (64 bits), the smallest number of dollars that can be represented is $0.01 divided by 2 to the power of 52 (the number of bits used to represent the fraction part of a double-precision number). This equals approximately **<span style="color:blue">$0.0000000000002384**.
|
Assuming double-precision floating-point numbers (64 bits), the smallest number of dollars that can be represented is $0.01 divided by 2 to the power of 52 (the number of bits used to represent the fraction part of a double-precision number). This equals approximately **$0.0000000000002384**.
|
||||||
|
|
||||||
Therefore, any calculation involving adding 0.01 to a dollar amount smaller than **<span style="color:blue">$0.0000000000002384** may yield wrong results due to the limited precision of floating-point arithmetic.
|
Therefore, any calculation involving adding 0.01 to a dollar amount smaller than **$0.0000000000002384** may yield wrong results due to the limited precision of floating-point arithmetic.
|
||||||
|
|
||||||
|
|
||||||
### 3. Name three practical differences between Python strings and bytes. Using sys.getsizeof, see how the sizes may be different. Do emoji change this answer?
|
### 3. Name three practical differences between Python strings and bytes. Using sys.getsizeof, see how the sizes may be different. Do emoji change this answer?
|
||||||
|
|
||||||
**<span style="color:blue"> Encoding**: Python strings are used to represent text data, while bytes are used to represent binary data. Strings are typically encoded using a character encoding such as UTF-8 or ASCII, which maps each character to a sequence of bytes, while bytes represent raw binary data that may not necessarily correspond to any particular character encoding.
|
** Encoding**: Python strings are used to represent text data, while bytes are used to represent binary data. Strings are typically encoded using a character encoding such as UTF-8 or ASCII, which maps each character to a sequence of bytes, while bytes represent raw binary data that may not necessarily correspond to any particular character encoding.
|
||||||
|
|
||||||
**<span style="color:blue"> Immutability**: Python strings are immutable, meaning that once a string is created, its contents cannot be changed. In contrast, bytes are mutable, meaning that their contents can be changed after they are created.
|
**Immutability**: Python strings are immutable, meaning that once a string is created, its contents cannot be changed. In contrast, bytes are mutable, meaning that their contents can be changed after they are created.
|
||||||
|
|
||||||
**<span style="color:blue"> Representation**: Python strings are represented using the str type, while bytes are represented using the bytes type. Strings are typically enclosed in quotes (' or ") and can contain any Unicode character, while bytes are enclosed in b quotes and can contain any binary data.
|
**Representation**: Python strings are represented using the str type, while bytes are represented using the bytes type. Strings are typically enclosed in quotes (' or ") and can contain any Unicode character, while bytes are enclosed in b quotes and can contain any binary data.
|
||||||
|
|
||||||
```
|
```
|
||||||
import sys
|
import sys
|
||||||
@@ -139,9 +139,9 @@ describe a hypothetical situation where modifying the CSV file to create a type
|
|||||||
|
|
||||||
####Answer:
|
####Answer:
|
||||||
|
|
||||||
**<span style="color:blue">Type confusion error** is a type of software vulnerability that occurs when a program assumes a variable is of a certain data type, but it is actually of a different data type. This can lead to unexpected behavior and even security vulnerabilities in some cases. For example, if a program assumes that a user input is an integer, but it is actually a string, the program may crash or produce incorrect results.
|
**Type confusion error** is a type of software vulnerability that occurs when a program assumes a variable is of a certain data type, but it is actually of a different data type. This can lead to unexpected behavior and even security vulnerabilities in some cases. For example, if a program assumes that a user input is an integer, but it is actually a string, the program may crash or produce incorrect results.
|
||||||
|
|
||||||
A hypothetical situation where modifying the CSV file would create a type confusion error is that if the initial information that has been written into the CSV is modified through **<span style="color:blue">string slicing**, for example, “Store4South350” sliced into “Store3, South, 350”. Though “350” looks like a numeric input, it remains a string. And this kind of input would cause type error in the read\_tomato\_sales().
|
A hypothetical situation where modifying the CSV file would create a type confusion error is that if the initial information that has been written into the CSV is modified through **string slicing**, for example, “Store4South350” sliced into “Store3, South, 350”. Though “350” looks like a numeric input, it remains a string. And this kind of input would cause type error in the read\_tomato\_sales().
|
||||||
|
|
||||||
|
|
||||||
#### 1. Prevent that nuclear war by modifying read_tomato_sales to perform better data validation.
|
#### 1. Prevent that nuclear war by modifying read_tomato_sales to perform better data validation.
|
||||||
@@ -158,8 +158,8 @@ read_tomato_sales("tomato_sales_error.csv")
|
|||||||
|
|
||||||
#### 2. Given today's conversation about character encodings, describe why you would want to use bytes instead of strings when processing data containing multiple human languages at the same time. Write a small Python function to demonstrate this.
|
#### 2. Given today's conversation about character encodings, describe why you would want to use bytes instead of strings when processing data containing multiple human languages at the same time. Write a small Python function to demonstrate this.
|
||||||
|
|
||||||
When processing data containing multiple human languages at the same time, it's often better to use bytes instead of strings. The reason for this is that **<span style="color:blue">bytes represent the raw data in its original form, while strings represent a specific encoding of the data.** When working with multiple languages, it's possible that the data may contain characters that are not supported by the encoding used by the string. **<span style="color:blue">This can result in data loss or incorrect character conversion.**
|
When processing data containing multiple human languages at the same time, it's often better to use bytes instead of strings. The reason for this is that **bytes represent the raw data in its original form, while strings represent a specific encoding of the data.** When working with multiple languages, it's possible that the data may contain characters that are not supported by the encoding used by the string. **This can result in data loss or incorrect character conversion.**
|
||||||
|
|
||||||
Using bytes instead of strings allows us to **<span style="color:blue">preserve the original data and avoid these conversion issues.** Bytes can be processed and encoded using specific character encodings only when necessary, and this can be done explicitly in a controlled way.
|
Using bytes instead of strings allows us to **preserve the original data and avoid these conversion issues.** Bytes can be processed and encoded using specific character encodings only when necessary, and this can be done explicitly in a controlled way.
|
||||||
|
|
||||||
By using bytes instead of strings, we can **<span style="color:blue">handle text in multiple languages without worrying about character encoding issues.** We can then decode the bytes using the appropriate encoding when necessary to display or process the text in a specific language.
|
By using bytes instead of strings, we can **handle text in multiple languages without worrying about character encoding issues.** We can then decode the bytes using the appropriate encoding when necessary to display or process the text in a specific language.
|
||||||
|
|||||||
Reference in New Issue
Block a user