Annotation of OpenXM_contrib2/asir2000/engine/real.c, Revision 1.6
1.3 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.4 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.6 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/real.c,v 1.5 2000/11/15 02:16:30 noro Exp $
1.3 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "base.h"
52: #include <math.h>
53:
54: double RatnToReal(a)
55: Q a;
56: {
57: double nm,dn,t,man;
58: int enm,edn,e;
59: unsigned int *p,s;
60:
61: nm = NatToReal(NM(a),&enm);
62: if ( INT(a) )
63: if ( enm >= 1 )
64: error("RatnToReal : Overflow");
65: else
66: return SGN(a)>0 ? nm : -nm;
67: else {
68: dn = NatToReal(DN(a),&edn);
69: man = nm/dn;
70: if ( SGN(a) < 0 )
71: man = -man;
72: if ( ((e = enm - edn) >= 1024) || (e <= -1023) )
73: error("RatnToReal : Overflow"); /* XXX */
74: else if ( !e )
75: return man;
76: else
77: return man*pow(2,e);
78: }
79: }
80:
81: double NatToReal(a,expo)
82: N a;
83: int *expo;
84: {
85: unsigned int *p;
86: int l,all,i,j,s,tb,top,tail;
87: unsigned int t,m[2];
88:
89: p = BD(a); l = PL(a) - 1;
90: for ( top = 0, t = p[l]; t; t >>= 1, top++ );
91: all = top + BSH*l; tail = (53-top)%BSH; i = l-(53-top)/BSH-1;
92: m[1] = i < 0 ? 0 : p[i]>>(BSH-tail);
93: for ( j = 1, i++, tb = tail; i <= l; i++ ) {
94: s = 32-tb; t = i < 0 ? 0 : p[i];
95: if ( BSH > s ) {
96: m[j] |= ((t&((1<<s)-1))<<tb);
97: if ( !j )
98: break;
99: else {
100: j--; m[j] = t>>s; tb = BSH-s;
101: }
102: } else {
103: m[j] |= (t<<tb); tb += BSH;
104: }
105: }
106: s = (all-1)+1023;
107: m[0] = (m[0]&((1<<20)-1))|(MIN(2046,s)<<20); *expo = MAX(s-2046,0);
108: #ifdef vax
109: t = m[0]; m[0] = m[1]; m[1] = t; itod(m);
110: #endif
1.2 noro 111: #if defined(__i386__) || defined(MIPSEL) || defined(VISUAL) || defined(__alpha) || defined(__FreeBSD__) || defined(__NetBSD__)
1.1 noro 112: t = m[0]; m[0] = m[1]; m[1] = t;
113: #endif
114: return *((double *)m);
115: }
116:
117: void addreal(a,b,c)
118: Num a,b;
119: Real *c;
120: {
121: double t;
122:
123: t = ToReal(a)+ToReal(b); MKReal(t,*c);
124: }
125:
126: void subreal(a,b,c)
127: Num a,b;
128: Real *c;
129: {
130: double t;
131:
132: t = ToReal(a)-ToReal(b); MKReal(t,*c);
133: }
134:
135: void mulreal(a,b,c)
136: Num a,b;
137: Real *c;
138: {
139: double t;
140:
141: if ( !a || !b )
142: *c = 0;
143: else {
144: t = ToReal(a)*ToReal(b);
1.5 noro 145: #if 0
1.1 noro 146: if ( !t )
147: error("mulreal : Underflow"); /* XXX */
148: else
1.5 noro 149: #endif
1.1 noro 150: MKReal(t,*c);
151: }
152: }
153:
154: void divreal(a,b,c)
155: Num a,b;
156: Real *c;
157: {
158: double t;
159:
160: if ( !a )
161: *c = 0;
162: else {
163: t = ToReal(a)/ToReal(b);
1.5 noro 164: #if 0
1.1 noro 165: if ( !t )
166: error("divreal : Underflow"); /* XXX */
167: else
1.5 noro 168: #endif
1.1 noro 169: MKReal(t,*c);
170: }
171: }
172:
173: void chsgnreal(a,c)
174: Num a,*c;
175: {
176: double t;
177: Real s;
178:
179: if ( !a )
180: *c = 0;
181: else if ( NID(a) == N_Q )
182: chsgnq((Q)a,(Q *)c);
183: else {
184: t = -ToReal(a); MKReal(t,s); *c = (Num)s;
185: }
186: }
187:
188: void pwrreal(a,b,c)
189: Num a,b;
190: Real *c;
191: {
192: double t;
193: double pwrreal0();
194:
195: if ( !b )
196: MKReal(1.0,*c);
197: else if ( !a )
198: *c = 0;
199: else if ( !RATN(b) || !INT(b) || (PL(NM((Q)b)) > 1) ) {
200: t = (double)pow((double)ToReal(a),(double)ToReal(b));
1.5 noro 201: #if 0
1.1 noro 202: if ( !t )
203: error("pwrreal : Underflow"); /* XXX */
204: else
1.5 noro 205: #endif
1.1 noro 206: MKReal(t,*c);
207: } else {
208: t = pwrreal0(BDY((Real)a),BD(NM((Q)b))[0]);
209: t = SGN((Q)b)>0?t:1/t;
1.5 noro 210: #if 0
1.1 noro 211: if ( !t )
212: error("pwrreal : Underflow"); /* XXX */
213: else
1.5 noro 214: #endif
1.1 noro 215: MKReal(t,*c);
216: }
217: }
218:
219: double pwrreal0(n,e)
220: double n;
221: int e;
222: {
223: double t;
224:
225: if ( e == 1 )
226: return n;
227: else {
228: t = pwrreal0(n,e / 2);
229: return e%2 ? t*t*n : t*t;
230: }
231: }
232:
233: int cmpreal(a,b)
234: Real a,b;
235: {
236: double t;
237:
238: t = ToReal(a)-ToReal(b);
239: return t>0.0 ? 1 : t<0.0?-1 : 0;
240: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>