Question

For certain values of float a and float b, expressions (a + b)*10 and a*10 +...

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%.

Homework Answers

Answer #1

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;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes...
Using Python: Create a code to generate 1001 values between 0 and 10 (essentially, start from...
Using Python: Create a code to generate 1001 values between 0 and 10 (essentially, start from 0 and use increment 0.01) and for each value ? ∈ [0,10] calculate sin(?) and the derivative of ?(x) = sin(?) using the central difference method: ????(?)/?? = ?(?+0.01)−?(?−0.01) 2∙0.01 . Output the result in a file using the print command: print (?, ???(?) , ????(?), ??? =′ ′, ??? =′\ ? ′ , ???? = ??) Recall that the output file must be...
A school counselor in a high school would like to try out a new conflict-resolution program...
A school counselor in a high school would like to try out a new conflict-resolution program to reduce aggressiveness in students. She first surveyed 16 students using a 20-item instrument to measure their levels of aggression (on a scale of 0 to 10, with higher numbers meaning higher aggression levels). One month after the conflict resolution program was implemented, the students were given the same survey. The data are listed in the table below. The school counselor/researcher has set the...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
a. If variance of asset A is 0.04 and variance of asset B is 0.02, what...
a. If variance of asset A is 0.04 and variance of asset B is 0.02, what is the correlation between the two assets? Assume covariance between the 2 assets to be 0.015. Show how you found the values. b. Suppose a portfolio has expected return of 15% and volatility of 30%. How can you combine this portfolio with the risk-free asset to create a portfolio with 10% expected return? Risk-free asset has expected return of 3%.  Show how you found the...
B. Dr. Quisling, a child psychologist, investigates play behavior in 4-year-olds. She is particularly interested in...
B. Dr. Quisling, a child psychologist, investigates play behavior in 4-year-olds. She is particularly interested in how young girls develop ideas about the gender appropriateness of certain toys. She conducts an experiment in which participants (4-year old girls) first interact with an adult for 10 minutes in a lab playroom. The adult models play for the child by interacting with a female-stereotyped toy (a Barbie doll), a male-stereotyped toy (a dump truck) or a gender-neutral toy (Legos). The children are...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...