Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function, ulps ( x , y ) , that takes two floating point parameters, x and y , and returns the number of
Write a function, ulpsxy that takes two floating point parameters, x and y and returns
the number of ulps floatingpoint intervals between x and y You will of course need to
take into account that x and y may have different exponents of the floatingpoint base in
their representation. Also, do not assume that x y handle either case Your function
may assume that its parameter will be the floatingpoint type. Do not use logarithms to
find the exponent of your floatingpoint numbers use repeated multiplicationdivision
following the pattern discussed in class. Your function should handle negative numbers
and numbers less than
Your function will return infinity if the input parameters have the following properties:
Opposite in signs, or
Either one of them is zero, or
Either one of them is either positive infinity or negative infinity.
If the input parameters are both negative, convert them to be positive numbers by taking
the absolute value. Your algorithm only needs to work with two positive floatingpoint
numbers.
The following code segment shows how to use math and sys modules to get the base,
infinity inf machine epsilon eps and mantissa digits prec in Python.
import sys
import math
base sysfloatinfo.radix
eps sysfloatinfo.epsilon
prec sysfloatinfo.mantdig
inf math.inf
Algorithm analysis:
Check the input parameters for special conditions.
Find the exponents for both input parameters in the machine base base
For example: Find exp such that
Examine the exp for both parameter:
a If they are the same: count the intervals between them. Remember that
the spacing is for each interval
b If they differ by one: add the intervals from the smaller number to
to the intervals from to the larger number.
c If they differ by more than one: In addition to the number of intervals in
part b add the numbers of intervals in the exponent ranges in between the
exponents of these two numbers.
Verify your algorithm with the following inputs with my answers which are listed after
each call:printulps
printulps
printulps
printulps
printulps
printulps
printulps
printulps
printulps
printulps
print
printulpsinf
printulpsinf
printulpsinf
printulps math.infinf
printulps
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started