| 1 |
/* Prototypes for regutil |
| 2 |
* |
| 3 |
* The contents of this file are subject to the Mozilla Public License |
| 4 |
* Version 1.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* http://www.mozilla.org/MPL/ |
| 7 |
* |
| 8 |
* Software distributed under the License is distributed on an "AS IS" |
| 9 |
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 10 |
* License for the specific language governing rights and limitations |
| 11 |
* under the License. |
| 12 |
* |
| 13 |
* The Original Code is regutil. |
| 14 |
* |
| 15 |
* $Header: /opt/cvs/Regina/regutil/regtrig.c,v 1.5 2014/06/22 03:53:33 mark Exp $ |
| 16 |
*/ |
| 17 |
/************************************************************************** |
| 18 |
* |
| 19 |
* Contains the following functions: |
| 20 |
* SysPi |
| 21 |
* SysSqrt |
| 22 |
* SysExp |
| 23 |
* SysLog |
| 24 |
* SysLog10 |
| 25 |
* SysSinH |
| 26 |
* SysCosH |
| 27 |
* SysTanH |
| 28 |
* SysPower |
| 29 |
* SysSin |
| 30 |
* SysCos |
| 31 |
* SysTan |
| 32 |
* SysCotan |
| 33 |
* SysArcSin |
| 34 |
* SysArcCos |
| 35 |
* SysArcTan |
| 36 |
* SysArcTan2 |
| 37 |
* |
| 38 |
* This is file contains the trigonometric functions found in the OOREXX project as |
| 39 |
* of 14 Jan 2008. |
| 40 |
* This file prepared by Michael Greene, January 2008 |
| 41 |
* |
| 42 |
------------------------------------------------------------------------------*/ |
| 43 |
/* */ |
| 44 |
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */ |
| 45 |
/* Copyright (c) 2005-2006 Rexx Language Association. All rights reserved. */ |
| 46 |
/* */ |
| 47 |
/* This program and the accompanying materials are made available under */ |
| 48 |
/* the terms of the Common Public License v1.0 which accompanies this */ |
| 49 |
/* distribution. A copy is also available at the following address: */ |
| 50 |
/* http://www.oorexx.org/license.html */ |
| 51 |
/* */ |
| 52 |
/* Redistribution and use in source and binary forms, with or */ |
| 53 |
/* without modification, are permitted provided that the following */ |
| 54 |
/* conditions are met: */ |
| 55 |
/* */ |
| 56 |
/* Redistributions of source code must retain the above copyright */ |
| 57 |
/* notice, this list of conditions and the following disclaimer. */ |
| 58 |
/* Redistributions in binary form must reproduce the above copyright */ |
| 59 |
/* notice, this list of conditions and the following disclaimer in */ |
| 60 |
/* the documentation and/or other materials provided with the distribution. */ |
| 61 |
/* */ |
| 62 |
/* Neither the name of Rexx Language Association nor the names */ |
| 63 |
/* of its contributors may be used to endorse or promote products */ |
| 64 |
/* derived from this software without specific prior written permission. */ |
| 65 |
/* */ |
| 66 |
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */ |
| 67 |
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ |
| 68 |
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */ |
| 69 |
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */ |
| 70 |
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */ |
| 71 |
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */ |
| 72 |
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ |
| 73 |
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */ |
| 74 |
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ |
| 75 |
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ |
| 76 |
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| 77 |
/* */ |
| 78 |
/******************************************************************************/ |
| 79 |
|
| 80 |
#include "regutil.h" |
| 81 |
#include <ctype.h> |
| 82 |
#include <math.h> |
| 83 |
|
| 84 |
/*********************************************************************/ |
| 85 |
/* Various definitions used by the math functions */ |
| 86 |
/*********************************************************************/ |
| 87 |
#define SINE 0 /* trig function defines... */ |
| 88 |
#define COSINE 3 /* the ordering is important, */ |
| 89 |
#define TANGENT 1 /* as these get transformed */ |
| 90 |
#define COTANGENT 2 /* depending on the angle */ |
| 91 |
#define MAXTRIG 3 /* value */ |
| 92 |
#define ARCSINE 0 /* defines for arc trig */ |
| 93 |
#define ARCCOSINE 1 /* functions. Ordering is */ |
| 94 |
#define ARCTANGENT 2 /* not as important here */ |
| 95 |
#define ARCTANGENT2 3 |
| 96 |
|
| 97 |
#define pi 3.14159265358979323846l /* pi value */ |
| 98 |
|
| 99 |
#define DEGREES 'D' /* degrees option */ |
| 100 |
#define RADIANS 'R' /* radians option */ |
| 101 |
#define GRADES 'G' /* grades option */ |
| 102 |
|
| 103 |
#define DEFAULT_PRECISION 9 /* default precision to use */ |
| 104 |
#define MAX_PRECISION 16 /* maximum available precision*/ |
| 105 |
|
| 106 |
static long ValidateMath(long numargs, RXSTRING args[], |
| 107 |
double *x, unsigned long *precision ); |
| 108 |
static long ValidateTrig(long numargs, RXSTRING args[], |
| 109 |
PRXSTRING retstr, int function ); |
| 110 |
static long ValidateArcTrig(long numargs, RXSTRING args[], |
| 111 |
PRXSTRING retstr, int function ); |
| 112 |
|
| 113 |
/*********************************************************************/ |
| 114 |
/* Function FormatResult: Common routine to format a floating point */ |
| 115 |
/* result for the math functions. */ |
| 116 |
/*********************************************************************/ |
| 117 |
/* formatted result required precision return string */ |
| 118 |
static void FormatResult(double res, unsigned long precision, PRXSTRING result ) |
| 119 |
{ |
| 120 |
if (res == 0) /* zero result? */ |
| 121 |
{ |
| 122 |
strcpy(result->strptr, "0"); /* make exactly 0 */ |
| 123 |
result->strlength = 1; |
| 124 |
} |
| 125 |
else |
| 126 |
/* format the result */ |
| 127 |
result->strlength = sprintf( result->strptr, "%.*f", precision, res ); |
| 128 |
/* end in a period? */ |
| 129 |
if (result->strptr[result->strlength - 1] == '.') |
| 130 |
result->strlength--; /* remove the period */ |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
/********************************************************************/ |
| 136 |
/* Functions: SysSqrt(), SysExp(), SysLog(), SysLog10, */ |
| 137 |
/* Functions: SysSinH(), SysCosH(), SysTanH() */ |
| 138 |
/* Description: Returns function value of argument. */ |
| 139 |
/* Input: One number. */ |
| 140 |
/* Output: Value of the function requested for arg. */ |
| 141 |
/* Returns 0 if the function executed OK, */ |
| 142 |
/* BADGENERAL otherwise. The interpreter will fail */ |
| 143 |
/* if the function returns a negative result. */ |
| 144 |
/* Notes: */ |
| 145 |
/* These routines take one to two parameters. */ |
| 146 |
/* The form of the call is: */ |
| 147 |
/* result = func_name(x <, prec> <,angle>) */ |
| 148 |
/* */ |
| 149 |
/********************************************************************/ |
| 150 |
|
| 151 |
rxfunc(syssqrt) |
| 152 |
{ |
| 153 |
double x; /* input number */ |
| 154 |
ULONG precision; /* precision used */ |
| 155 |
LONG rc; /* function return code */ |
| 156 |
|
| 157 |
/* validate the inputs */ |
| 158 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 159 |
if (!rc) /* good function call */ |
| 160 |
/* format the result */ |
| 161 |
FormatResult(sqrt(x), precision, result); |
| 162 |
return rc; /* return error code */ |
| 163 |
} |
| 164 |
|
| 165 |
/*==================================================================*/ |
| 166 |
|
| 167 |
rxfunc(sysexp) |
| 168 |
{ |
| 169 |
double x; /* input number */ |
| 170 |
ULONG precision; /* precision used */ |
| 171 |
LONG rc; /* validation return code */ |
| 172 |
|
| 173 |
/* validate the inputs */ |
| 174 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 175 |
if (!rc) /* good function call */ |
| 176 |
/* format the result */ |
| 177 |
FormatResult(exp(x), precision, result); |
| 178 |
return rc; /* return error code */ |
| 179 |
} |
| 180 |
|
| 181 |
/*==================================================================*/ |
| 182 |
|
| 183 |
rxfunc(syslog) |
| 184 |
{ |
| 185 |
double x; /* input number */ |
| 186 |
ULONG precision; /* precision used */ |
| 187 |
LONG rc; /* validation return code */ |
| 188 |
|
| 189 |
/* validate the inputs */ |
| 190 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 191 |
if (!rc) /* good function call */ |
| 192 |
/* format the result */ |
| 193 |
FormatResult(log(x), precision, result); |
| 194 |
return rc; /* return error code */ |
| 195 |
} |
| 196 |
|
| 197 |
/*==================================================================*/ |
| 198 |
|
| 199 |
rxfunc(syslog10) |
| 200 |
{ |
| 201 |
double x; /* input number */ |
| 202 |
ULONG precision; /* precision used */ |
| 203 |
LONG rc; /* validation return code */ |
| 204 |
|
| 205 |
/* validate the inputs */ |
| 206 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 207 |
if (!rc) /* good function call */ |
| 208 |
/* format the result */ |
| 209 |
FormatResult(log10(x), precision, result); |
| 210 |
return rc; /* return error code */ |
| 211 |
} |
| 212 |
|
| 213 |
/*==================================================================*/ |
| 214 |
|
| 215 |
rxfunc(syssinh) |
| 216 |
{ |
| 217 |
double x; /* input number */ |
| 218 |
ULONG precision; /* precision used */ |
| 219 |
LONG rc; /* validation return code */ |
| 220 |
|
| 221 |
/* validate the inputs */ |
| 222 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 223 |
if (!rc) /* good function call */ |
| 224 |
/* format the result */ |
| 225 |
FormatResult(sinh(x), precision, result); |
| 226 |
return rc; /* return error code */ |
| 227 |
} |
| 228 |
|
| 229 |
/*==================================================================*/ |
| 230 |
|
| 231 |
rxfunc(syscosh) |
| 232 |
{ |
| 233 |
double x; /* input number */ |
| 234 |
ULONG precision; /* precision used */ |
| 235 |
LONG rc; /* validation return code */ |
| 236 |
|
| 237 |
/* validate the inputs */ |
| 238 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 239 |
if (!rc) /* good function call */ |
| 240 |
/* format the result */ |
| 241 |
FormatResult(cosh(x), precision, result); |
| 242 |
return rc; /* return error code */ |
| 243 |
} |
| 244 |
|
| 245 |
/*==================================================================*/ |
| 246 |
|
| 247 |
rxfunc(systanh) |
| 248 |
{ |
| 249 |
double x; /* input number */ |
| 250 |
ULONG precision; /* precision used */ |
| 251 |
LONG rc; /* validation return code */ |
| 252 |
|
| 253 |
/* validate the inputs */ |
| 254 |
rc = ValidateMath(argc, argv, &x, &precision); |
| 255 |
if (!rc) /* good function call */ |
| 256 |
/* format the result */ |
| 257 |
FormatResult(tanh(x), precision, result); |
| 258 |
return rc; /* return error code */ |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/********************************************************************/ |
| 263 |
/* Functions: SysPower() */ |
| 264 |
/* Description: Returns function value of arguments. */ |
| 265 |
/* Input: Two numbers. */ |
| 266 |
/* Output: Value of the x to the power y. */ |
| 267 |
/* Returns 0 if the function executed OK, */ |
| 268 |
/* -1 otherwise. The interpreter will fail */ |
| 269 |
/* if the function returns a negative result. */ |
| 270 |
/* Notes: */ |
| 271 |
/* This routine takes two to three parameters. */ |
| 272 |
/* The form of the call is: */ |
| 273 |
/* result = func_name(x, y <, prec>) */ |
| 274 |
/* */ |
| 275 |
/********************************************************************/ |
| 276 |
|
| 277 |
rxfunc(syspower) |
| 278 |
{ |
| 279 |
double x; /* input number */ |
| 280 |
double y; /* second input number */ |
| 281 |
ULONG precision; /* precision used */ |
| 282 |
LONG rc; /* validation code */ |
| 283 |
char *prec; |
| 284 |
|
| 285 |
rc = 0; /* set default completion */ |
| 286 |
precision = DEFAULT_PRECISION; /* set max digits count */ |
| 287 |
|
| 288 |
if (argc < 2 || /* no arguments */ |
| 289 |
argc > 3 || |
| 290 |
!RXVALIDSTRING(argv[0]) || /* first is omitted */ |
| 291 |
!RXVALIDSTRING(argv[1])) /* second is omitted */ |
| 292 |
rc = BADGENERAL; /* this is invalid */ |
| 293 |
/* convert input number */ |
| 294 |
else if (sscanf(argv[0].strptr, " %lf", &x) != 1) |
| 295 |
rc = BADGENERAL; /* this is invalid */ |
| 296 |
/* convert second input */ |
| 297 |
else if (sscanf(argv[1].strptr, " %lf", &y) != 1) |
| 298 |
rc = BADGENERAL; /* this is invalid */ |
| 299 |
if (!rc) { /* good function call */ |
| 300 |
if (argc == 3) { /* have a precision */ |
| 301 |
rxstrdup(prec,argv[2]); |
| 302 |
precision = atol(prec); |
| 303 |
} |
| 304 |
/* keep to maximum */ |
| 305 |
precision = min(precision, MAX_PRECISION); |
| 306 |
/* format the result */ |
| 307 |
FormatResult(pow(x,y), precision, result); |
| 308 |
} |
| 309 |
return rc; /* return error code */ |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/********************************************************************/ |
| 314 |
/* Functions: RxSin(), RxCos(), RxTan(), RxCotan() */ |
| 315 |
/* Description: Returns trigonometric angle value. */ |
| 316 |
/* Input: Angle in radian or degree or grade */ |
| 317 |
/* Output: Trigonometric function value for Angle. */ |
| 318 |
/* Returns 0 if the function executed OK, */ |
| 319 |
/* -1 otherwise. The interpreter will fail */ |
| 320 |
/* if the function returns a negative result. */ |
| 321 |
/* Notes: */ |
| 322 |
/* These routines take one to three parameters. */ |
| 323 |
/* The form of the call is: */ |
| 324 |
/* x = func_name(angle <, prec> <, [R | D | G]>) */ |
| 325 |
/* */ |
| 326 |
/********************************************************************/ |
| 327 |
|
| 328 |
rxfunc(syssin) |
| 329 |
{ |
| 330 |
/* call common routine */ |
| 331 |
return ValidateTrig(argc, argv, result, SINE); |
| 332 |
} |
| 333 |
|
| 334 |
/*==================================================================*/ |
| 335 |
|
| 336 |
rxfunc(syscos) |
| 337 |
{ |
| 338 |
/* call common routine */ |
| 339 |
return ValidateTrig(argc, argv, result, COSINE); |
| 340 |
} |
| 341 |
|
| 342 |
/*==================================================================*/ |
| 343 |
|
| 344 |
rxfunc(systan) |
| 345 |
{ |
| 346 |
/* call common routine */ |
| 347 |
return ValidateTrig(argc, argv, result, TANGENT); |
| 348 |
} |
| 349 |
|
| 350 |
/*==================================================================*/ |
| 351 |
|
| 352 |
rxfunc(syscotan) |
| 353 |
{ |
| 354 |
/* call common routine */ |
| 355 |
return ValidateTrig(argc, argv, result, COTANGENT); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
/********************************************************************/ |
| 360 |
/* Functions: SysPi() */ |
| 361 |
/* Description: Returns value of pi for given precision */ |
| 362 |
/* Input: Precision. Default is 9 */ |
| 363 |
/* Output: Value of the pi to given precision */ |
| 364 |
/* Notes: */ |
| 365 |
/* This routine takes one parameters. */ |
| 366 |
/* The form of the call is: */ |
| 367 |
/* result = syspi(<precision>) */ |
| 368 |
/* */ |
| 369 |
/********************************************************************/ |
| 370 |
|
| 371 |
rxfunc(syspi) |
| 372 |
{ |
| 373 |
ULONG precision; /* required precision */ |
| 374 |
char *prec; |
| 375 |
|
| 376 |
checkparam(0, 1); |
| 377 |
|
| 378 |
precision = DEFAULT_PRECISION; /* set default precision */ |
| 379 |
if (argc == 1 ) { |
| 380 |
rxstrdup(prec,argv[0]); |
| 381 |
precision = atol(prec); |
| 382 |
} |
| 383 |
/* keep to maximum */ |
| 384 |
precision = min(precision, MAX_PRECISION); |
| 385 |
/* format the result */ |
| 386 |
FormatResult(pi, precision, result); /* format the result */ |
| 387 |
return 0; /* good result */ |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
/********************************************************************/ |
| 392 |
/* Functions: SysArcSin(), SysArcCos(), SysArcTan() */ |
| 393 |
/* Description: Returns angle from trigonometric value. */ |
| 394 |
/* Input: Angle in radian or degree or grade */ |
| 395 |
/* Output: Angle for matching trigonometric value. */ |
| 396 |
/* Returns 0 if the function executed OK, */ |
| 397 |
/* -1 otherwise. The interpreter will fail */ |
| 398 |
/* if the function returns a negative result. */ |
| 399 |
/* Notes: */ |
| 400 |
/* These routines take one to three parameters. */ |
| 401 |
/* The form of the call is: */ |
| 402 |
/* a = func_name(arg <, prec> <, [R | D | G]>) */ |
| 403 |
/* */ |
| 404 |
/********************************************************************/ |
| 405 |
|
| 406 |
rxfunc(sysarcsin) |
| 407 |
{ |
| 408 |
/* call common routine */ |
| 409 |
return ValidateArcTrig(argc, argv, result, ARCSINE); |
| 410 |
} |
| 411 |
|
| 412 |
/*==================================================================*/ |
| 413 |
|
| 414 |
rxfunc(sysarccos) |
| 415 |
{ |
| 416 |
/* call common routine */ |
| 417 |
return ValidateArcTrig(argc, argv, result, ARCCOSINE); |
| 418 |
} |
| 419 |
|
| 420 |
/*==================================================================*/ |
| 421 |
|
| 422 |
rxfunc(sysarctan) |
| 423 |
{ |
| 424 |
/* call common routine */ |
| 425 |
return ValidateArcTrig(argc, argv, result, ARCTANGENT); |
| 426 |
} |
| 427 |
|
| 428 |
rxfunc(sysarctan2) |
| 429 |
{ |
| 430 |
/* call common routine */ |
| 431 |
return ValidateArcTrig(argc, argv, result, ARCTANGENT2); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
/*********************************************************************/ |
| 439 |
/* Function ValidateMath: Common validation routine for math */ |
| 440 |
/* that are of the form fn(number, <precision>) */ |
| 441 |
/*********************************************************************/ |
| 442 |
/* Number of arguments. Function arguments. input number returned precision */ |
| 443 |
|
| 444 |
static long ValidateMath(long argc, |
| 445 |
RXSTRING argv[], |
| 446 |
double *x, |
| 447 |
unsigned long *precision ) |
| 448 |
{ |
| 449 |
LONG rc; /* validation code */ |
| 450 |
char *prec; |
| 451 |
|
| 452 |
rc = 0; /* set default completion */ |
| 453 |
*precision = DEFAULT_PRECISION; /* set max digits count */ |
| 454 |
|
| 455 |
if (argc < 1 || /* no arguments */ |
| 456 |
argc > 2 || |
| 457 |
!RXVALIDSTRING(argv[0])) /* first is omitted */ |
| 458 |
rc = BADGENERAL; /* this is invalid */ |
| 459 |
/* convert input number */ |
| 460 |
else if (sscanf(argv[0].strptr, " %lf", x) != 1) |
| 461 |
rc = BADGENERAL; /* this is invalid */ |
| 462 |
|
| 463 |
if ( argc == 2 ) { |
| 464 |
rxstrdup(prec,argv[1]); |
| 465 |
*precision = atol(prec); |
| 466 |
} |
| 467 |
/* truncate to maximum */ |
| 468 |
*precision = min(*precision, MAX_PRECISION); |
| 469 |
return rc; /* return success code */ |
| 470 |
} |
| 471 |
|
| 472 |
/*********************************************************************/ |
| 473 |
/* Function ValidateTrig: Common validation routine for math */ |
| 474 |
/* that are of the form fn(number, <precision>, <unit>) */ |
| 475 |
/*********************************************************************/ |
| 476 |
static long ValidateTrig(long argc,/* Number of arguments. */ |
| 477 |
RXSTRING argv[], /* Function arguments. */ |
| 478 |
PRXSTRING result, /* return string */ |
| 479 |
int function ) /* function to perform */ |
| 480 |
{ |
| 481 |
LONG rc; /* validation code */ |
| 482 |
int units; /* angle type */ |
| 483 |
double angle; /* working angle */ |
| 484 |
double nsi; /* convertion factor */ |
| 485 |
double nco; /* convertion factor */ |
| 486 |
ULONG precision; /* returned precision */ |
| 487 |
double res; /* result */ |
| 488 |
char *prec; |
| 489 |
|
| 490 |
rc = 0; /* set default completion */ |
| 491 |
precision = DEFAULT_PRECISION; /* set max digits count */ |
| 492 |
units = DEGREES; /* default angle is degrees */ |
| 493 |
nsi = 1.; /* set default conversion */ |
| 494 |
nco = 1.; /* set default conversion */ |
| 495 |
|
| 496 |
if (argc < 1 || /* no arguments */ |
| 497 |
argc > 3 || |
| 498 |
!RXVALIDSTRING(argv[0])) /* first is omitted */ |
| 499 |
rc = BADGENERAL; /* this is invalid */ |
| 500 |
/* convert input number */ |
| 501 |
else if (sscanf(argv[0].strptr, " %lf", &angle) != 1) |
| 502 |
rc = BADGENERAL; /* this is invalid */ |
| 503 |
else if (argc == 3) { /* have an option */ |
| 504 |
if (RXZEROLENSTRING(argv[2])) /* null string? */ |
| 505 |
rc = BADGENERAL; /* this is invalid */ |
| 506 |
else { /* process the options */ |
| 507 |
/* get the option character */ |
| 508 |
units = toupper(argv[2].strptr[0]); |
| 509 |
/* was it a good option? */ |
| 510 |
if (units != DEGREES && units != RADIANS && units != GRADES) |
| 511 |
rc = BADGENERAL; /* bad option is error */ |
| 512 |
} |
| 513 |
} |
| 514 |
if (!rc) { /* everything went well? */ |
| 515 |
/* truncate to maximum */ |
| 516 |
if (argc >= 2 ) { /* have a precision */ |
| 517 |
rxstrdup(prec,argv[1]); |
| 518 |
precision = atol(prec); |
| 519 |
} |
| 520 |
rc = 0; |
| 521 |
precision = min(precision, MAX_PRECISION); |
| 522 |
if (units == DEGREES) { /* need to convert degrees */ |
| 523 |
nsi = (angle < 0.) ? -1. : 1.; /* get the direction */ |
| 524 |
angle = fmod(fabs(angle), 360.); /* make modulo 360 */ |
| 525 |
if (angle <= 45.) /* less than 45? */ |
| 526 |
angle = angle * pi / 180.; |
| 527 |
else if (angle < 135.) { /* over on the other side? */ |
| 528 |
angle = (90. - angle) * pi / 180.; |
| 529 |
function = MAXTRIG - function; /* change the function */ |
| 530 |
nco = nsi; /* swap around the conversions*/ |
| 531 |
nsi = 1.; |
| 532 |
} |
| 533 |
else if (angle <= 225.) { /* around the other way? */ |
| 534 |
angle = (180. - angle) * pi / 180.; |
| 535 |
nco = -1.; |
| 536 |
} |
| 537 |
else if (angle < 315.) { /* close to the origin? */ |
| 538 |
angle = (angle - 270.) * pi / 180.; |
| 539 |
function = MAXTRIG - function; /* change the function */ |
| 540 |
nco = -nsi; |
| 541 |
nsi = 1.; |
| 542 |
} |
| 543 |
else |
| 544 |
angle = (angle - 360.) * pi / 180.; |
| 545 |
} |
| 546 |
else if (units == GRADES) { /* need to convert degrees */ |
| 547 |
nsi = (angle < 0.) ? -1. : 1.; /* get the direction */ |
| 548 |
angle = fmod(fabs(angle), 400.); /* make modulo 400 */ |
| 549 |
if (angle <= 50.) |
| 550 |
angle = angle * pi / 200.; |
| 551 |
else if (angle < 150.) { |
| 552 |
angle = (100. - angle) * pi / 200.; |
| 553 |
function = MAXTRIG - function; /* change the function */ |
| 554 |
nco = nsi; /* swap the conversions */ |
| 555 |
nsi = 1.; |
| 556 |
} |
| 557 |
else if (angle <= 250.) { |
| 558 |
angle = (200. - angle) * pi / 200.; |
| 559 |
nco = -1.; |
| 560 |
} |
| 561 |
else if (angle < 350.) { |
| 562 |
angle = (angle - 300.) * pi / 200.; |
| 563 |
function = MAXTRIG - function; /* change the function */ |
| 564 |
nco = -nsi; |
| 565 |
nsi = 1.; |
| 566 |
} |
| 567 |
else |
| 568 |
angle = (angle - 400.) * pi / 200.; |
| 569 |
} |
| 570 |
switch (function) { /* process the function */ |
| 571 |
case SINE: /* Sine function */ |
| 572 |
res = nsi * sin(angle); |
| 573 |
break; |
| 574 |
case COSINE: /* Cosine function */ |
| 575 |
res = nco * cos(angle); |
| 576 |
break; |
| 577 |
case TANGENT: /* Tangent function */ |
| 578 |
res = nsi * nco * tan(angle); |
| 579 |
break; |
| 580 |
case COTANGENT: /* cotangent function */ |
| 581 |
/* overflow? */ |
| 582 |
if ((res = tan(angle)) == 0.0) |
| 583 |
rc = BADGENERAL; /* this is an error */ |
| 584 |
else |
| 585 |
res = nsi * nco / res; /* real result */ |
| 586 |
break; |
| 587 |
} |
| 588 |
if (!rc) /* good result? */ |
| 589 |
/* format the result */ |
| 590 |
FormatResult(res, precision, result); |
| 591 |
} |
| 592 |
return rc; /* return success code */ |
| 593 |
} |
| 594 |
|
| 595 |
/*********************************************************************/ |
| 596 |
/* Function ValidateATrig: Common validation routine for math */ |
| 597 |
/* that are of the form fn(number, <precision>, <units>) */ |
| 598 |
/*********************************************************************/ |
| 599 |
/* Number of arguments. Function arguments. return string function to perform */ |
| 600 |
|
| 601 |
static long ValidateArcTrig(long argc, |
| 602 |
RXSTRING argv[], |
| 603 |
PRXSTRING result, |
| 604 |
int function ) |
| 605 |
{ |
| 606 |
LONG rc; /* validation code */ |
| 607 |
int units; /* angle type */ |
| 608 |
double angle; /* working angle */ |
| 609 |
double nsi; /* convertion factor */ |
| 610 |
double nco; /* convertion factor */ |
| 611 |
ULONG precision; /* returned precision */ |
| 612 |
double x; /* input number */ |
| 613 |
double y; /* input number */ |
| 614 |
char *prec; |
| 615 |
int maxargs,minargs; |
| 616 |
int precoff, unitoff; |
| 617 |
|
| 618 |
rc = 0; /* set default completion */ |
| 619 |
precision = DEFAULT_PRECISION; /* set max digits count */ |
| 620 |
units = DEGREES; /* default angle is degrees */ |
| 621 |
nsi = 1.; /* set default conversion */ |
| 622 |
nco = 1.; /* set default conversion */ |
| 623 |
|
| 624 |
if ( function == ARCTANGENT2 ) |
| 625 |
{ |
| 626 |
minargs = 2; |
| 627 |
maxargs = 4; |
| 628 |
precoff = 2; |
| 629 |
unitoff = 3; |
| 630 |
} |
| 631 |
else |
| 632 |
{ |
| 633 |
minargs = 1; |
| 634 |
maxargs = 3; |
| 635 |
precoff = 1; |
| 636 |
unitoff = 2; |
| 637 |
} |
| 638 |
if (argc < minargs || /* no arguments */ |
| 639 |
argc > maxargs || |
| 640 |
!RXVALIDSTRING(argv[0])) /* first is omitted */ |
| 641 |
rc = BADGENERAL; /* this is invalid */ |
| 642 |
/* convert input number */ |
| 643 |
else if (sscanf(argv[0].strptr, " %lf", &x) != 1) |
| 644 |
rc = BADGENERAL; /* this is invalid */ |
| 645 |
else if (argc == maxargs) { /* have a unit option */ |
| 646 |
if (RXZEROLENSTRING(argv[unitoff])) /* null string? */ |
| 647 |
rc = BADGENERAL; /* this is invalid */ |
| 648 |
else { /* process the options */ |
| 649 |
/* get the option character */ |
| 650 |
units = toupper(argv[unitoff].strptr[0]); |
| 651 |
/* was it a good option? */ |
| 652 |
if (units != DEGREES && units != RADIANS && units != GRADES) |
| 653 |
rc = BADGENERAL; /* bad option is error */ |
| 654 |
} |
| 655 |
} |
| 656 |
if ( function == ARCTANGENT2 |
| 657 |
&& sscanf(argv[1].strptr, " %lf", &y) != 1) |
| 658 |
rc = BADGENERAL; /* this is invalid */ |
| 659 |
if (!rc) { /* everything went well? */ |
| 660 |
if (argc > minargs) { /* have a precision */ |
| 661 |
rxstrdup(prec,argv[precoff]); |
| 662 |
precision = atol(prec); |
| 663 |
} |
| 664 |
/* truncate to maximum */ |
| 665 |
precision = min(precision, MAX_PRECISION); |
| 666 |
switch (function) { /* process the function */ |
| 667 |
case ARCSINE: /* ArcSine function */ |
| 668 |
angle = asin(x); |
| 669 |
break; |
| 670 |
case ARCCOSINE: /* ArcCosine function */ |
| 671 |
angle = acos(x); |
| 672 |
break; |
| 673 |
case ARCTANGENT: /* ArcTangent function */ |
| 674 |
angle = atan(x); |
| 675 |
break; |
| 676 |
case ARCTANGENT2: /* ArcTangent2 function */ |
| 677 |
angle = atan2(x,y); |
| 678 |
break; |
| 679 |
default: |
| 680 |
/* should not get here */ |
| 681 |
angle = 0.0; |
| 682 |
break; |
| 683 |
} |
| 684 |
if (units == DEGREES) /* have to convert the result?*/ |
| 685 |
angle = angle * 180. / pi; /* make into degrees */ |
| 686 |
else if (units == GRADES) /* need it in grades? */ |
| 687 |
angle = angle * 200. / pi; /* convert to base 400 */ |
| 688 |
/* format the result */ |
| 689 |
FormatResult(angle, precision, result); |
| 690 |
} |
| 691 |
return rc; /* return success code */ |
| 692 |
} |
| 693 |
|