File: pow.c

package info (click to toggle)
starplot 0.95.5-8.2%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,476 kB
  • sloc: ansic: 11,296; cpp: 6,418; sh: 5,092; makefile: 613; yacc: 289; sed: 16
file content (35 lines) | stat: -rw-r--r-- 711 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "compat.h"

#if ! HAVE_POW
#include <math.h>
#include <stdio.h>

double
pow(double x, double y)
{
  int i;
  double result = 1.0, frac_y;
  
  if (x == 0 && y <= 0) {
    fprintf(stderr, _("Sorry, division by zero was encountered.\n"));
    exit(EXIT_FAILURE);
  }
  if (x == 0) return 0;
  if (y < 0) x = 1.0/x, y *= -1.0;
  frac_y = y - (int)y;
  
  for (i = 0; i < (int)y; i++) result *= x;

  /* if y is integer, avoid calling expensive exp(), log() functions */
  if (frac_y != 0) {
    if (x >= 0) result *= exp(frac_y * log(x));
    else {
      fprintf(stderr,
	_("Sorry, fractional power of a negative number was attempted.\n"));
      exit(EXIT_FAILURE);
    }
  }
  return result;
}
#endif