By default, Matlab stores all numeric variables as double-precision floating-poi
ID: 3783857 • Letter: B
Question
By default, Matlab stores all numeric variables as double-precision floating-point values using 64 bits. However you can force Matlab to use less bits by using functions like: int16() 16 bit integer (whole) number int32() 32 bit integer (whole) number single() 32 but floating point (real) number A. Demonstrate overflow, numbers that get too big. Write two similar arithmetic expressions where one result just fits in the data type and the other is too large for Matlab to store Do this for the default double-precision floating-point values, then int16(), int32() and single() B. Demonstrate underflow, real numbers that get too small and result is zero. Write two similar arithmetic expressions where one result is very small but nonzero and the other is too small for Matlab to store. Do this for the default double-precision floating-point values, then single(). Use format long to display as many digits as possible. C. Demonstrate a rounding error with repeated real number arithmetic statements. D. Demonstrate the non-associative nature of basic arithmetic by adding the same very small numbers to a very large number, but grouping the terms in two different ways showing different results.Explanation / Answer
Demonstration of Overflow:
>> doublevar = 32768
doublevar = 32768
>> int16var = int16(32768)
int16var = 32767 %Maximum number which could be stored is 32767 using 16 bits
>> singlevar = single(1.5e+39)
singlevar = Inf
>> doublevar = double(1.5e+39)
doublevar = 1.5000e+039
>> int32var = int32(2147483648)
int32var = 2147483647
>> doublevar = 2147483648
doublevar = 2.1475e+009
Demonstration of Underflow:
doublesmall = 2.2251e-323
doublesmall = 2.4703e-323
>> doubleverysmall = 2.2251e-324
doubleverysmall = 0
>> singlesmall = single(1.5e-45)
singlesmall = 1.4013e-045
>> singleverysmall = single(1.5e-46)
singleverysmall = 0
Simple Demostration of Rounding error:
>> data = 1/3
data = 0.33333
>> data == 0.33333
ans = 0
Simple Demonstration of Non-Associativity:
>> res1 = (0.1 + 0.2) + 0.3
res1 = 0.6
>> res2 = 0.1 + ( 0.2 + 0.3)
res2 = 0.6
>> sprintf('%.17f',res1)
ans = 0.60000000000000009
>> sprintf('%.17f',res2)
ans = 0.59999999999999998
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.