| 2.1 |
1.1875 in binary is 0001.111. 1.1 in binary is 1.00111111111111111, etc.
Ruby cannot do the calculation exactly because it cannot store 1.001 in binary
exactly.
|
| |
| 2.2 |
>> 10.0**10000.0
=> Infinity
>> 0.0/0.0
=> NaN
|
| 2.3 |
One combination Ruby allows to add is floats and ints.
|
| |
| 2.4 |
a)
1
3
5
7
9
b)
9
6
3
0
|
| 2.5 |
Open Ended
|
| |
| 2.6 |
Open Ended. Answer to this problem will depend on the equation and where the
paranthesis are placed. An equation written as x*y/z+2 will give a different result
than x*(y/z)+2. However, if the paranthesis were placed as (x*y)/z+2, the answer
will be identical to the equation without paranthesis. This deals with the order
of operations.
|
| |
| 2.7 |
Depending on the values of tempc1 and tempc2, the value for changeTemp may turn o
ut to be negative. To ensure changeTemp is always positive, we could add another
line
changeTemp = changeTemp.abs
after the current line or simply change the current line to
changeTemp= (tempc1-tempc2).abs
|
| |
| 2.8 |
a) false
b) false
c) false
|
| 2.9 |
a) -4
b)3
c)-2
|
| |
| 2.10 |
a)2
b)4
c)-1
|
| |
| 2.11 |
hey hey hey yo yo
|
| |
| 2.12 |
5.5, float
|
| |
| 2.13 |
a) 4
b) 3.6
|
| |
| 2.14 |
Nothing. An error will result due to the improper input of a scientific notation
number for y
|
| |
| 2.15 |
There are two problems with the above line. The temperature for summer is never used
and instead the spring temperature is used twice. Also, only tempF is being divided
by 4. We must put brackets around the 4 variables if we want all the variables to be
divided by 4.
The correct line should look like
avgTemp=(tempW+tempS+temps+tempF)/4
|
| |
| 2.16 |
"hellogoodbye"
|
| |
| 2.17 |
Nothing. We cannot add a string to an int so an error will occur.
|
| |
| 2.18 |
>> Rational(1001,1000) - 1
=> Rational1, 1000
>> (Rational(1001,1000) - 1).to_s
=> "1/1000"
Yes, it is computed exactly. Ruby can compute exactly because it does not have
to deal with fractional numbers directly. Instead, it converts everything to exact
fractions, which use whole numbers, and thus can have perfect accuracy.
|
| |
| 2.19 |
If strings can be converted to numbers automatically, user input is much easier to
deal with, since conversion is one implicitly. However, the "+" operator can be
ambiguous, since "1.2" + "2.3" does not equal "1.22.3" anymore. If number types
are stricter, you are warned when you lose the fractional part of a number. However,
Ruby, due to its flexible typing will not have this problem, but you may have a
float when you expected an integer.
|
| |
| 2.20 |
To correct the code, Line 4 should be if(a==b).
It is very easy to mix up assignment (=) and equality (==). In the current code,
line 4 sets variable a equal to b so both of them hold the value “gold”. Now that
both variables hold the same value, line 5 is always reached even if the 2 strings
initially were not equal.
|
| |
| 2.21 |
A syntax error is an error caused by wrong syntax written in program whereas a logic
error occurs when a program yields results that are logically inconsistent with what
the results should be. There are many correct examples to give for each error type
so long as they are consistent with the definition. Common examples of syntax errors
include forgetting to use quotes for strings or misspelling a method call. An
example of a logic error would be using an incorrect math equation to calculate a
certain value in a program.
|
| |
| 2.22 |
In lines 2 and 3 the quotation marks need to be switched to the other hellos
|
| |
| 2.23 |
a) 0
b) 0.0
|
| 2.24 |
remove the comma from 1,000
|
| |
| 2.25 |
4
|
| |
| 2.26 |
line 4: 9
line 5: 8.5
|
| |
| 2.27 |
a,b, and c result in errors. d results in 5
|