-
Notifications
You must be signed in to change notification settings - Fork 13
/
floating_in_text.Rmd
72 lines (56 loc) · 1.56 KB
/
floating_in_text.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
jupyter:
orphan: true
jupytext:
notebook_metadata_filter: all,-language_info
split_at_heading: true
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.13.7
kernelspec:
display_name: Python 3
language: python
name: python3
prereqs:
- pathlib
---
# Formats for floating point values in text files
Let's say we have a floating point numbers like this:
```{python}
a_number = 314.15926
a_number
```
We can also represent these numbers in exponential format. Exponential format
breaks the number into a two parts: the *significand*; and the *exponent*.
The significand is a floating point number with one digit before a decimal
point. The exponent is an integer. For example:
```{python}
exp_number = 3.1415926E2
exp_number
```
Here the significand is `3.1415926`, and the exponent is `2`, the value after
the `E`. The number is given by `s * 10 ** e` where `s` is the significand and
`e` is the exponent. In this case: `314.15926 = 3.1415926 * 10 ** 2`.
This exponential format is the default format that `np.savetxt` uses to
represent floating point numbers when writing to text files. For example:
```{python}
import numpy as np
an_array = np.array([a_number, 1.0, 2.0])
an_array
```
```{python}
# Save the array as a text file.
np.savetxt('some_numbers.txt', an_array)
```
```{python}
# Show the text in the file
from pathlib import Path
pth = Path('some_numbers.txt')
print(pth.read_text())
```
Finally, clean up the temporary file:
```{python}
pth.unlink()
```