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

  • Thread starter Thread starter GreenPrint
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around the behavior of the int8 data type in programming, specifically in the context of calculating the first 10 terms of the harmonic series using an array of integers. Participants explore the implications of using int8 for arithmetic operations and the resulting output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant expresses confusion about the results obtained when using int8 to store integers and calculate the harmonic series, noting that the output is not as expected.
  • Another participant clarifies that the harmonic series terms are being rounded to the nearest integer due to the int8 type, which limits precision.
  • A further response discusses the implications of type handling in programming languages, explaining that the type of operands influences the precision of operations, leading to integer results when using int8.
  • Examples from other programming languages (C, Java, Pascal) are provided to illustrate how implicit typing affects arithmetic results, emphasizing the importance of understanding these behaviors in programming.

Areas of Agreement / Disagreement

Participants generally agree on the impact of the int8 type on the results, but there is some uncertainty regarding the specific reasons for the rounding behavior and the broader implications of type handling across different programming languages.

Contextual Notes

Limitations include assumptions about the behavior of int8 in various programming contexts and the potential for rounding errors in calculations. The discussion does not resolve the nuances of these behaviors across different programming languages.

Who May Find This Useful

Readers interested in programming, particularly those working with data types and arithmetic operations in languages like MATLAB, C, and Java, may find this discussion relevant.

GreenPrint
Messages
1,186
Reaction score
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
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.
 
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.
 
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:

Similar threads

Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
5K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K