SINFONI Pipeline Reference Manual  2.6.0
sinfo_ipow.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 /*
23  * This function is so generic and used everywhere, it diserves its
24  * own source file...
25  */
26 /*
27  $Id: sinfo_ipow.c,v 1.4 2012-03-02 08:42:20 amodigli Exp $
28  $Author: amodigli $
29  $Date: 2012-03-02 08:42:20 $
30  $Revision: 1.4 $
31  */
53 #include "sinfo_ipow.h"
54 double sinfo_ipow(double x, int p)
55 {
56 
57  /* Get rid of trivial cases */
58  switch (p) {
59  case 0:
60  return 1.00 ;
61 
62  case 1:
63  return x ;
64 
65  case 2:
66  return x*x ;
67 
68  case 3:
69  return x*x*x ;
70 
71  case -1:
72  return 1.00 / x ;
73 
74  case -2:
75  return (1.00 / x) * (1.00 / x) ;
76  }
77  double r;
78  if (p>0) {
79  r = x ;
80  while (--p) r *= x ;
81  } else {
82  double recip;
83  r = recip = 1.00 / x ;
84  while (++p) r *= recip ;
85  }
86  return r;
87 }
88 
89