2.2 – Arithmetic Instructions and type conversion

by on August 13, 2017

Types of arithmetic instructions:

Arithmetic instruction is a combination of constants, variables and arithmetic operators that yields a value. In the previous session we have discussed presidency, associativity of arithmetic operators and evaluation of an arithmetic expression. Now we will address some of the issues regarding to types in arithmetic expressions. According to the type of operands arithmetic instructions are classified into

• Integer Mode Arithmetic Instructions
• Real Mode Arithmetic Instructions
• Mixed Mode Arithmetic Instructions

Integer Mode Arithmetic Instruction:

In an arithmetic expression, if every operand is of integer type then the instruction is called integer mode arithmetic instruction. It always yields an integer result

Example:

```#include<stdio.h>
int main()
{
float x,y;
x=5/2;/* yields only 2 instead of 2.5 */
y=2/5;/* yields only 0 instead of 0.4 */
printf("%f\n%f",x,y);
return 0;
}
```

Output:
2.000000
0.000000

Example explained:
In x=5/2; both 5 and 2 are integer constants that yields an integer value 2 instead 2.5.
In y=2/5; both 2 and 5 are integer constants that yield an integer value 0 instead 0.4

Real Mode Arithmetic Instruction:

In an arithmetic expression, if every operand is of real type then the instruction is called real mode arithmetic instruction. It always yields a real result.

```#include<stdio.h>
int main()
{
float x,y;
x=5.0/2.0;/* yields 2.5 */
y=2.0/5.0;/* yields 0.4 */
printf("%f\n%f",x,y);
return 0;
}
```

Output:
2.500000
0.400000

Example explained:
In x=5.0/2.0; both 5.0 and 2.0 are real constants that yields a real value 2.5
In y=2.0/5.0; both 2.0 and 5.0 are real constants that yields a real value 0.4

Mixed Mode Arithmetic Instruction:

In an arithmetic expression, if operands are of both integer and real types then the instruction is called mixed mode arithmetic instruction. It always yields a real value as result.

```#include<stdio.h>
int main()
{
float x,y;
x=5/2.0;/* yields 2.5 */
y=2.0/5;/* yields 0.4 */
printf("%f\n%f",x,y);
return 0;
}
```

Output:
2.500000
0.400000

Example explained:

In a mixed mode instruction if any one operand is of real type then that yields a real constant.

Guess the result of following instructions:

1. 5/2
2. 2/5
3. 5.0/2
4. 2/5.0
5. 5.0/2.0
6. 2.0/5.0
Show Result
1. 2
2. 0
3. 2.5
4. 0.4
5. 2.5
6. 0.4

Type conversion:

Some times the type of expression (r-value) and the type of variable (l-type) may not be same, in such case r-value may be promote or demote automatically to the l-type.

```#include<stdio.h>
int main()
{
int x;
x=12.45;
printf("%d",x);
return 0;
}
```

Output:
12

In the above example because x is an int type of variable, it can only store integer part of 12.45 that is 12. Here real value is demoting to int value.

```#include<stdio.h>
int main()
{
float x;
x=5/2;
printf("%f",x);
return 0;
}
```

Output:
2.000000

In the above example 5/2 is an integer arithmetic expression yields only integer that is 2. It will be assigned to float variable x by promoting into float type 2.000000. Though there is no error in the above program, it gives incorrect answer 2.000000 instead of 2.500000. It can be rectified by changing the expression into real type by adding fractional part .0 to either 5 or 2.

```#include<stdio.h>
int main()
{
float x;
x=5.0/2;
printf("%f",x);
return 0;
}
```

Output:
2.500000

All the times, it may not be possible to convert an integer expression into real expression by adding fractional part .0 because fractional part .0 can be added to any constant but can’t be added to a variable.

```#include<stdio.h>
int main()
{
float x;
int a=5,b=2;
x=a/b.0;/* Incorrect */
printf("%f",x);
return 0;
}
```

Output:
hello.c: In function ‘main’:
hello.c:7:8: error: expected ‘;’ before numeric constant
x=a/b.0;
^

The above program throws an error because fractional part can’t be directly added to the variable. It can be rectified by explicitly promoting a variable from one type to another using cast operator

Syntax:

(Target type) variable

```x=(float)a/b;
```

It yields a real value because a is first promoted to float type, dividing which by int value b results a real value

```x=a/(float)b;
```

It yields a real value because b is first promoted to float type. Dividing an int value by real value results a real value

Rectifying the above program:

```#include<stdio.h>
int main()
{
float x;
int a=5,b=2;
x=a/(float)b;
printf("%f",x);
return 0;
}
```

Output:
2.500000

Guess the result of following program:

```#include<stdio.h>
int main()
{
float c,f;
f=42;
c=5/9*(f-32);
printf(" Temperature in calicoes %f",c);
return 0;
}
```
Show Result
Output:
Temperature in calicoes 0.000000

oh.. it is unexpected, the reason is 5/9 is an integer mode expression yields an integer value 0, multiplies (f-32) with 0 yields 0. It can be rectified by either adding a fractional part .0 to either 5 or 9. In other way we can change the type using cast operator (float).

Rectified program:

```#include<stdio.h>
int main()
{
float c,f;
f=42;
c=(float)5/9*(f-32);/* 5 is promoting to float type*/
printf(" Temperature in calicoes %f",c);
return 0;
}
```

Output:
Temperature in calicoes 5.555556

Another way of writing same program:

```#include<stdio.h>
int main()
{
float c,f;
f=42;
c=(f-32)*5/9;
printf(" Temperature in calicoes %f",c);
return 0;
}
```

Output:
Temperature in calicoes 5.555556

Here we are using another technique were f-32 yields a real value 10.000000, multiplying which with 5 yields a real value and dividing which by 9 yields a real value.

Previous post: