-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path3-3_objetos_EN.r
115 lines (102 loc) · 4.21 KB
/
3-3_objetos_EN.r
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
################################################################################
### R BASICS WORKSHOP ###
### EXERCISE 3.2: The First Circle of the R Inferno: falling into the ###
### Floating Point Trap ###
### ###
### ###
### Center for Conservation and Sustainable Development ###
### Missouri Botanical Garden ###
### Website: rbasicsworkshop.weebly.com ###
################################################################################
### INTRODUCTION ###############################################################
# According to Dante, an inscription posted at the gates of Hell hints at what
# lies beyond:
#
# "Per me si va ne la città dolente,
# per me si va ne l’etterno dolore,
# per me si va tra la perduta gente..."
#
# The First Circle of the R Inferno is "home of the virtuous pagans. These are
# people who live in ignorance of the Floating Point Gods" (page 9 in Burns, P.
# 2011. The R Inferno).
################################################################################
## TASK 1 ##
# Finite representation of numbers results in numerical error.
# The floating point representation approximates real numbers
# with limited precision. Examine the finite representation
# of 1/3, using the argument "digits" of the function "print"
# (see the help page of function "print" by typing in the
# console "?print" or "help(print)"):
1/3
print(1/3, digits=10)
print(1/3, digits=16)
## TASK 2 ##
# Finite representation of numbers afects operations.
# Examine the two numbers below, many would likely
# think they seem equal:
0.1
0.3/3
# yet, see what happens when performing an operation to ask if
# they are, indeed, equal:
0.1 == 0.3/3
# examine the difference between the two numbers
0.1 - 0.3/3
# take a closer look at the finite representation of each number:
print(0.1, digits=16)
print(0.3/3, digits=16)
## TASK 3 ##
# Explore the precision of your computer. In all cases below you
# know the answer should be larger than zero. See when your computer
# yields zero:
1e-10
print(1e-10, digits=20)
1e-11
print(1e-11, digits=20)
1e-12
print(1e-12, digits=20)
1e-20
print(1e-20, digits=20)
1e-40
print(1e-40, digits=20)
#keep increasing the magnitude of the exponent until you get zero
## TASK 4 ##
# Similar to the previous task. In all cases below you know the answer
# is "TRUE". See when your computer yields the wrong answer:
1+1e10 > 1e10
1+1e11 > 1e11
1+1e12 > 1e12
#keep increasing the magnitude of the exponent until you get "FALSE"
## TASK 5 ##
# When does infinity start? Just after the largest possible floating
# point number. Explore the begining of infinity: sequentially
# increase the floating point number, until you reach infinity:
1e300
1e301
1e302
1e303
1e304
1e305
1e306
1e307
1e308
1e309
# keep in mind, however, that operations with infinity yield meaningful
# results, by example:
log(0)
exp(-Inf)
Inf/0
0/Inf
## TASK 6 ##
# Learn the bottom line: "R is good enough at hiding numerical error
# that it is easy to forget that it is there. Don't forget." (page 11
# in Burns, P. 2011. The R Inferno).
#
# Whenever carrying out operations on numbers represented as floating point
# numbers (non-integer real numbers), be careful. Comparison of such numbers
# with operators such as "==" might cause trouble. When performing operations
# on numbers that are suppossed to be integers, it may be helpful to use function
# "round" to make sure the computer is representing the numbers as integers. Visit
# the help page for this function by typing in the console "?round" or "help(round)".
# For further discussion of numerical error and the floating point representation see:
# http://www.burns-stat.com/documents/tutorials/impatient-r/more-r-key-objects/more-r-numbers/
# http://www.math.pitt.edu/~trenchea/math1070/MATH1070_2_Error_and_Computer_Arithmetic.pdf