Exercises






Exercises

Exercise 1

Write a program that determines whether your implementation

  1. Sets errno

  2. Raises a floating-point exception

when a floating-point error occurs in the library functions.

Exercise 2

Check the results of the following function calls:

  1. fmod(1.0, 0.0)

  2. acosh(0.5)

  3. atan2(0.0, 0.0)

  4. log(-1.0)

  5. sqrt(-1.0)

  6. tgamma(-1.0)

  7. laguerre(1, -1.0)

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

  1. What are the normalized representations of the following values?

    1. .1

    2. 1

    3. 10

    4. 100

  2. What are the normalized representations of the values of the following expressions?

    1. .600 x 103 + .600 x 103

    2. .500 x 100 + .500 x 10-1

    3. .500 x 100 + .500 x 10-2

    4. .500 x 100 + .500 x 10-3

    5. .100 x 101 + .100 x 10x1

    6. .123 x 103 + .123 x 102

    7. .123 x 103 + .123 x 101

    8. .123 x 103 + .123 x 100

  3. Assuming that the floating-point representation does not support de-normalized values, what are the results of the following expressions?

    1. .123 x 10-8/.100 x 102

    2. .123 x 10-8/.100 x 103

    3. .123 x 10-8/.100 x 104

    4. .123 x 10-8/.100 x 105

  4. Assuming now that the floating-point representation does support de-normalized values, what are the results of the following expressions?

    1. .123 x 10-8/.100 x 102

    2. .123 x 10-8/.100 x 103

    3. .123 x 10-8/.100 x 104

    4. .123 x 10-8/.100 x 105

Exercise 4

Write a program that uses the macros defined in the header <float.h> (or <cfloat>) to determine the following properties of each of the three types float, double, long double for your implementation:

  1. The radix of the floating-point representation

  2. The number of digits, in base radix, in the significand

  3. The minimum and maximum exponents for nonzero normalized representable values

  4. The number of decimal digits representable in the significand

  5. The minimum positive representable value

  6. The 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<double>::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:

  1. A function int dround(double) that uses the default rounding mode to return its argument rounded to an integer value

  2. A 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 thrown

  3. A 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 result

    Make 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. 1.0

  2. 0.0 3. numeric_limits<double>::infinity()

  3. -numeric_limits<double>::infinity()

  4. numeric_limits<double>::quiet_NaN()

  5. numeric_limits<double>::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<double>, and verify that it works sensibly.

Exercise 19

What is the type of the returned value of each of the following function calls?

  1. abs(1)

  2. abs(1.0)

  3. cos(1)

  4. cos(1.0F)

  5. cos(1.0)

  6. cos(1.0L)

  7. cosf(1.0L)

  8. cosl(1)

  9. fmax(1.0, 2.0)

  10. fmax(1.0F, 2.0F)

  11. fmax(1.0F, 2.0)

  12. fmax(1, 2.0)

  13. fmax(1, 2.0F)

Write a program to verify your answers.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows