Voder-Vocoder

The Log of Hal Canary

Navigation: Home | THE LOG | Log Archives | Resume | Contact Info | Public Key | SSL | Math Applets | Site Map | WP Backend | RSS2 | Atom

« | »

rounding functions for POSIX bc.

$ echo 'scale=10; 1/3*3;'|bc
.9999999999

A work in progress, rounding functions for POSIX bc.

/* DTPD */
define round(x,dec) {
  oscale = scale;
  x = x * (10 ^ dec);
  scale = 0;
  tmp = x / 1;
  if (x-tmp >= 0.5) {
    x = tmp + 1 ;
  } else {
    x = tmp ;
  }
  scale = oscale;
  x = x / (10 ^ dec);
  return x;
}
define round(x) {
  oscale = scale;
  dec = oscale-1;
  x = x * (10 ^ dec);
  scale = 0;
  tmp = x / 1;
  if (x-tmp >= 0.5) {
    x = tmp + 1 ;
  } else {
    x = tmp ;
  }
  scale = oscale;
  x = x / (10 ^ dec);
  return x;
}

Hal Canary | Computers & Code | 2009-08-14 12:26:54 UTC
Permanent Link Comments Off | No Comments

Comments are closed.
Email me your comments — h3 at halcanary dot org.

Copyright 1997-2007 by Hal Canary.
mailto: h3 at halcanary dot org
xmpp:halcanary@jabber.org
aim:halwcanary
http://halcanary.org