Explaining the Results of int8(x) and the First 10 Terms of the Harmonic Series

  • Thread starter GreenPrint
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses defining an array of the first 10 integers using the int8 type designation and using these integers to calculate the first 10 terms in the harmonic series. The results obtained are explained to be due to rounding to the nearest integer, which is a common practice in many computer languages. The reason for this is to save space in storage and it can lead to less accuracy and round off errors in large calculations. The use of the int8 command in this case implicitly types the result of the operation, determining the precision of the result. Being aware of how a programming language handles implicit typing of operation results is important for understanding the language.
  • #1
GreenPrint
1,196
0

Homework Statement



"Define an array of the first 10 integers, using the int8 type designation. Use these integers to calculate the first 10 terms in the harmonic series. Explain your results." I don't understand why I get the results I do and was wondering if someone could explain. I know that the int8(x) stores x as an integer as one byte but I still can't seem to explain why I get the results I do. Thanks in advance!

for k=1:10
a(k)=int8(k);
end
b=1./a

b =

1 1 0 0 0 0 0 0 0 0

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
The harmonic series is : (1 1/2 1/3 1/4 1/5 ...), that is, (1.00 0.50 0.33 0.25 0.20 ...). Obviously it's just rounding to the nearest integer.
 
  • #3
Thanks. What is the reason in doing this though? I thought all the int8 command did was compress how much space is used for storage. I understand that this will cause less accuracy and round off errors when doing large calculations but I don't understand why it rounded exactly.
 
  • #4
It's a fairly common thing in a lot of computer languages that the "type" of the operands on the RHS of an assignment determine the precision of the operation performed, which is int8 in this case. So even though you haven't explicitly typed "b" as int8 it gets implicitly typed by the assignment.

You get similar things in C and java, where for example the operation x=13/4 returns x=3.0, even when x is of type float (though they would return 3.25 if you entered the expression as 13.0/4 for example).

Pascal however would return 3.25 in either case, because it relies on things other than the types of the LHS to determine the type of the result. Matlab would also return 3.25 if you typed it in with 13 and 4 literally, because it treats everything as a double precision floating point by default (that is whether or not you type 13 or 13.0 it's still type float). If however you entered "x=int8(13);, x/4" then you'd just get the integer result of 3

Being aware of how a language handles this kind of implicit typing of operation results is a really important part of the understanding of any programming language.
 
Last edited:
  • #5


The int8(x) function is used to convert a value or variable x into an 8-bit integer. This means that the value of x is limited to a range of -128 to 127. In the provided code, the array a is defined as int8 integers from 1 to 10. This means that the values in the array can only range from -128 to 127, and any value outside of this range will be truncated to fit within the 8-bit limit.

When the values in array a are used to calculate the harmonic series, the values are divided by 1, resulting in the same values as before. However, since the values are now being stored as 8-bit integers, any decimal values are truncated and only the integer portion is displayed. This is why the first two terms of the harmonic series, 1 and 1/2, remain the same, but the rest of the terms become 0 due to the truncation of decimal values.

To further explain, the harmonic series is a divergent series, meaning that the sum of its terms increases without bound. This means that as we continue to add more terms, the values become increasingly larger. However, since we are using int8 integers, the values are limited to the range of -128 to 127. This causes the values to overflow and wrap around within this range, resulting in the values becoming 0.

In summary, the results of int8(x) and the first 10 terms of the harmonic series are a result of the limited range of values that can be stored as 8-bit integers. The truncation of decimal values and the overflow of values within the range of -128 to 127 explain the results obtained.
 

FAQ: Explaining the Results of int8(x) and the First 10 Terms of the Harmonic Series

What is the purpose of the "int8" function in MATLAB?

The "int8" function in MATLAB is used to convert a numeric value to an 8-bit signed integer. This can be useful for saving memory or when working with specific data types in MATLAB.

How does the "int8" function differ from other data type conversion functions in MATLAB?

The "int8" function specifically converts a numeric value to an 8-bit signed integer, whereas other data type conversion functions in MATLAB may convert to different data types such as double, single, or uint8.

Can the "int8" function be used with arrays or matrices in MATLAB?

Yes, the "int8" function can be used with arrays or matrices in MATLAB. It will convert each individual element to an 8-bit signed integer.

Are there any limitations to using the "int8" function in MATLAB?

Yes, the "int8" function can only convert numeric values within the range of -128 to 127. Any values outside of this range will result in an error.

Can the "int8" function be reversed to convert an 8-bit signed integer back to a numeric value?

Yes, the "int8" function can be reversed by using the "double" or "single" function in MATLAB to convert the 8-bit signed integer back to a numeric value with the appropriate data type.

Similar threads

Replies
21
Views
2K
Replies
3
Views
1K
Replies
3
Views
3K
Replies
4
Views
2K
Back
Top