# Exercises

### Exercises

#### Exercise 1

 Write a program that determines whether your implementation Sets errnoRaises a floating-point exception when a floating-point error occurs in the library functions.

#### Exercise 2

 Check the results of the following function calls: fmod(1.0, 0.0)acosh(0.5)atan2(0.0, 0.0)log(-1.0)sqrt(-1.0)tgamma(-1.0)laguerre(1, -1.0)legendre(1, 2.0)

#### Exercise 3

 Assume a floating-point representation with β = 10 and p = 3. Thus, normalized values can be represented in the usual exponential notation: .ddd x 10e. Also assume that the maximum allowable exponent is 9 and that the minimum allowable exponent is - 9. What are the normalized representations of the following values?.1110100What are the normalized representations of the values of the following expressions?.600 x 103 + .600 x 103.500 x 100 + .500 x 10-1.500 x 100 + .500 x 10-2.500 x 100 + .500 x 10-3.100 x 101 + .100 x 10x1.123 x 103 + .123 x 102.123 x 103 + .123 x 101.123 x 103 + .123 x 100Assuming that the floating-point representation does not support de-normalized values, what are the results of the following expressions?.123 x 10-8/.100 x 102.123 x 10-8/.100 x 103.123 x 10-8/.100 x 104.123 x 10-8/.100 x 105Assuming now that the floating-point representation does support de-normalized values, what are the results of the following expressions?.123 x 10-8/.100 x 102.123 x 10-8/.100 x 103.123 x 10-8/.100 x 104.123 x 10-8/.100 x 105

#### Exercise 4

 Write a program that uses the macros defined in the header (or ) to determine the following properties of each of the three types float, double, long double for your implementation: The radix of the floating-point representationThe number of digits, in base radix, in the significandThe minimum and maximum exponents for nonzero normalized representable valuesThe number of decimal digits representable in the significandThe minimum positive representable valueThe maximum representable finite value

#### Exercise 5

 Write a program that uses std::numeric_limits to determine the properties listed in the previous exercise.

#### Exercise 6

 Write a program that computes the smallest value of type double that can be added to 1.0 to produce a value that is different from 1.0. Is this value the same as numeric_limits::epsilon()? Should it be?

#### Exercise 7

 Write a program that determines which of the standard rounding modes are supported by your implementation.

#### Exercise 8

 Write a program that converts each of the values -1.5, -0.5, 0.5, and 1.5 to an integer value, using each of the rounding modes supported by your implementation. Explain the results.

#### Exercise 9

 Write a program that rounds each of the values -1.5, -0.5, 0.5, and 1.5, using the default rounding mode and calling each of the functions ceil, floor, nearbyint, rint, lrint, llrint, round, lround, llround, and TRunc.

#### Exercise 10

 Write a program that consists of the following functions: A function int dround(double) that uses the default rounding mode to return its argument rounded to an integer valueA function int uround(double) that uses the function lrint to round its argument upward and return the result; on return the default rounding mode should be unchanged, even if an exception was thrownA main function that calls dround(1.1) and displays the result, then calls uround(1.1) and displays the result, and, finally, calls dround (1.1) again and displays the resultMake sure that the two calls to dround return the same value.

#### Exercise 11

 Write a program that determines which of the standard floating-point exceptions are supported by your implementation.

#### Exercise 12

 Write a program that performs mathematical operations that raise each of the floating-point exceptions supported by your implementation and verifies that the exception was raised.

#### Exercise 13

 Write a program containing a function that raises a floating-point exception of your choice and a second function that calls the first but guarantees that on return, all floating-point exceptions are cleared, even if the return is through a C++ exception. The main function should check that the floating-point exceptions are cleared.

#### Exercise 14

 Write a program containing a function that raises a floating-point exception of your choice and a second function that calls the first but guarantees that on return, the floating-point status has been restored to its state on entry to the function, even if the return is through a C++ exception. The main function should check that the floating-point status is unchanged.

#### Exercise 15

 Write a program containing a function that raises a floating-point exception of your choice and a second function that calls the first but defers raising of floating-point exceptions until it returns.

#### Exercise 16

 Write a program containing a function that changes the rounding mode and raises a floating-point exception of your choice and a second function that calls the first but guarantees that on return, the floating-point status and the rounding mode have been restored to their states on entry to the function, even if the return is through a C++ exception. The main function should check that the floating-point status and the rounding mode are unchanged.

#### Exercise 17

 Write a function template that returns a string object that holds "infinite", "not-a-number", "normal", "denormal", or "zero", depending on the value of its argument. Test it with the following values: 1.00.0 3. numeric_limits::infinity()-numeric_limits::infinity()numeric_limits::quiet_NaN()numeric_limits::denorm_min()

#### Exercise 18

 Write a comparison function that takes two arguments of type double and imposes a strict weak ordering on all possible argument values. In particular, make sure that it handles NaN values consistently. Write a program that uses this function as the comparator for a std::set, and verify that it works sensibly.

#### Exercise 19

 What is the type of the returned value of each of the following function calls? abs(1)abs(1.0)cos(1)cos(1.0F)cos(1.0)cos(1.0L)cosf(1.0L)cosl(1)fmax(1.0, 2.0)fmax(1.0F, 2.0F)fmax(1.0F, 2.0)fmax(1, 2.0)fmax(1, 2.0F) Write a program to verify your answers.