MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
Division resets the scale of a number to the value of scale. This can be used as follows to
extract the integer portion of a number.
define integer_part(x) {
# a local to save the value of scale
auto old_scale
# save the old scale, and set scale to 0
old_scale = scale; scale=0
# divide by 1 to truncate the number
x/=1
# restore the old scale
scale=old_scale
return (x)
}
Having defined this function, it is now trivial to define one to return the fractional part of a
number.
define fractional_part(x) {
return (x - integer_part(x))
}
The following function lets you set the scale of a number to a given number of decimal places.
define set_scale(x, s) {
auto os
os = scale
scale = s
x/=1
scale = os
return (x)
}
set_scale() can now be used in a function which rounds a number to two decimal places.
define round2(num) {
auto temp;
if(scale(num) < 2) return (set_scale(num, 2))
temp = (num - set_scale(num, 2)) * 1000
if(temp > 5) num += 0.01
return (set_scale(num,2))
}
Commands and Utilities 1-61