Why are the elements of an array formatted as zeros when they are multiplied by 1/2 or 1/3?

In Fortran 1/2 is an integer division operation which will round down to, in this case, 0. Same for 1/3. If you want a real result, do a real division operation, such as 1.0/2.0. Note that assigning the result of 1/2 to a real variable will set the real variable to 0.0, that is the integer division will result in 0 and the assignment, which happens next, will cast that value to its nearest real representation.

This business of integer division producing integer results is very common in programming languages.

Leave a Comment