For certain values of float a and float b, expressions (a +
b)*10 and a*10 + b*10 can differ by more than 20% even when both a
and b are of the same magnitude. Please provide a program in C with
variables a and b that confirm this claim.
You must prove the statement without going out of bounds! I.e. a*10
or b*10 or (a+b)*10 should never exceed the max/min floating-point
value.
Demo run:
a=...
b=...
(a+b)*10 = ...
a*10 + b*10 = ...
Difference: xx%
Requirements: your solution must match the demo run. Values a and b must be of the same magnitude and not extreme (i.e. should not be too small or too big).
Hints: you might want to use %e specifier in your program.
Two numbers of the same order of magnitude have roughly the same
scale: the larger value is less than ten times the smaller
value.
This means Bigger_Number / Smaller_Number < 10
This also applies to negative numbers: -20 and 3 are of the same
magnitude!
the % difference of two numbers?
The formula is:
(Bigger_Value-Smaller_Value)*100/Smaller_Value
If you compare 15 and 10, you'll have (15-10)/10 = 0.5 or 50%.
SOLUTION
INPUT $ OUTPUT
CODE FOR GIVEN PROBLEM:
#include <stdio.h>
int main()
{
float a,b,c,d,xx;
a = 33.333333;
b = 66.666666;
c = (a+b)*10;
d = a*10 + b*10;
xx = c - d;
printf("%e\n",xx);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.