[BACK]Return to real.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / engine

Annotation of OpenXM_contrib2/asir2000/engine/real.c, Revision 1.10

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.10    ! fujimoto   48:  * $OpenXM: OpenXM_contrib2/asir2000/engine/real.c,v 1.9 2015/08/06 10:01:52 fujimoto 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: {
1.7       noro       57:        double nm,dn,man;
1.1       noro       58:        int enm,edn,e;
                     59:
                     60:        nm = NatToReal(NM(a),&enm);
                     61:        if ( INT(a) )
1.7       noro       62:                if ( enm >= 1 ) {
1.1       noro       63:                        error("RatnToReal : Overflow");
1.7       noro       64:                        /* NOTREACHED */
                     65:                        return 0;
                     66:                } else
1.1       noro       67:                        return SGN(a)>0 ? nm : -nm;
                     68:        else {
                     69:                dn = NatToReal(DN(a),&edn);
                     70:                man = nm/dn;
                     71:                if ( SGN(a) < 0 )
                     72:                        man = -man;
1.7       noro       73:                if ( ((e = enm - edn) >= 1024) || (e <= -1023) ) {
1.1       noro       74:                        error("RatnToReal : Overflow"); /* XXX */
1.7       noro       75:                        /* NOTREACHED */
                     76:                        return 0;
                     77:                } else if ( !e )
1.1       noro       78:                        return man;
                     79:                else
                     80:                        return man*pow(2,e);
                     81:        }
                     82: }
                     83:
                     84: double NatToReal(a,expo)
                     85: N a;
                     86: int *expo;
                     87: {
                     88:        unsigned int *p;
                     89:        int l,all,i,j,s,tb,top,tail;
                     90:        unsigned int t,m[2];
                     91:
                     92:        p = BD(a); l = PL(a) - 1;
                     93:        for ( top = 0, t = p[l]; t; t >>= 1, top++ );
                     94:        all = top + BSH*l; tail = (53-top)%BSH; i = l-(53-top)/BSH-1;
                     95:        m[1] = i < 0 ? 0 : p[i]>>(BSH-tail);
                     96:        for ( j = 1, i++, tb = tail; i <= l; i++ ) {
                     97:                s = 32-tb; t = i < 0 ? 0 : p[i];
                     98:                if ( BSH > s ) {
                     99:                        m[j] |= ((t&((1<<s)-1))<<tb);
                    100:                        if ( !j )
                    101:                                break;
                    102:                        else {
                    103:                                j--; m[j] = t>>s; tb = BSH-s;
                    104:                        }
                    105:                } else {
                    106:                        m[j] |= (t<<tb); tb += BSH;
                    107:                }
                    108:        }
                    109:        s = (all-1)+1023;
                    110:        m[0] = (m[0]&((1<<20)-1))|(MIN(2046,s)<<20); *expo = MAX(s-2046,0);
                    111: #ifdef vax
                    112:        t = m[0]; m[0] = m[1]; m[1] = t; itod(m);
                    113: #endif
1.10    ! fujimoto  114: #if defined(__i386__) || defined(MIPSEL) || defined(VISUAL) || defined(__MINGW32__) || defined(__alpha) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__x86_64)
1.1       noro      115:        t = m[0]; m[0] = m[1]; m[1] = t;
                    116: #endif
                    117:        return *((double *)m);
                    118: }
                    119:
                    120: void addreal(a,b,c)
                    121: Num a,b;
                    122: Real *c;
                    123: {
                    124:        double t;
                    125:
                    126:        t = ToReal(a)+ToReal(b); MKReal(t,*c);
                    127: }
                    128:
                    129: void subreal(a,b,c)
                    130: Num a,b;
                    131: Real *c;
                    132: {
                    133:        double t;
                    134:
                    135:        t = ToReal(a)-ToReal(b); MKReal(t,*c);
                    136: }
                    137:
                    138: void mulreal(a,b,c)
                    139: Num a,b;
                    140: Real *c;
                    141: {
                    142:        double t;
                    143:
                    144:        if ( !a || !b )
                    145:                *c = 0;
                    146:        else {
                    147:                t = ToReal(a)*ToReal(b);
1.5       noro      148: #if 0
1.1       noro      149:                if ( !t )
                    150:                        error("mulreal : Underflow"); /* XXX */
                    151:                else
1.5       noro      152: #endif
1.1       noro      153:                        MKReal(t,*c);
                    154:        }
                    155: }
                    156:
                    157: void divreal(a,b,c)
                    158: Num a,b;
                    159: Real *c;
                    160: {
                    161:        double t;
                    162:
                    163:        if ( !a )
                    164:                *c = 0;
                    165:        else {
                    166:                t = ToReal(a)/ToReal(b);
1.5       noro      167: #if 0
1.1       noro      168:                if ( !t )
                    169:                        error("divreal : Underflow"); /* XXX */
                    170:                else
1.5       noro      171: #endif
1.1       noro      172:                        MKReal(t,*c);
                    173:        }
                    174: }
                    175:
                    176: void chsgnreal(a,c)
                    177: Num a,*c;
                    178: {
                    179:        double t;
                    180:        Real s;
                    181:
                    182:        if ( !a )
                    183:                *c = 0;
                    184:        else if ( NID(a) == N_Q )
                    185:                chsgnq((Q)a,(Q *)c);
                    186:        else {
                    187:                t = -ToReal(a); MKReal(t,s); *c = (Num)s;
                    188:        }
                    189: }
                    190:
                    191: void pwrreal(a,b,c)
                    192: Num a,b;
                    193: Real *c;
                    194: {
                    195:        double t;
                    196:        double pwrreal0();
                    197:
                    198:        if ( !b )
                    199:                MKReal(1.0,*c);
                    200:        else if ( !a )
                    201:                *c = 0;
                    202:        else if ( !RATN(b) || !INT(b) || (PL(NM((Q)b)) > 1) ) {
                    203:                t = (double)pow((double)ToReal(a),(double)ToReal(b));
1.5       noro      204: #if 0
1.1       noro      205:                if ( !t )
                    206:                        error("pwrreal : Underflow"); /* XXX */
                    207:                else
1.5       noro      208: #endif
1.1       noro      209:                        MKReal(t,*c);
                    210:        } else {
                    211:                t = pwrreal0(BDY((Real)a),BD(NM((Q)b))[0]);
                    212:                t = SGN((Q)b)>0?t:1/t;
1.5       noro      213: #if 0
1.1       noro      214:                if ( !t )
                    215:                        error("pwrreal : Underflow"); /* XXX */
                    216:                else
1.5       noro      217: #endif
1.1       noro      218:                        MKReal(t,*c);
                    219:        }
                    220: }
                    221:
                    222: double pwrreal0(n,e)
                    223: double n;
                    224: int e;
                    225: {
                    226:        double t;
                    227:
                    228:        if ( e == 1 )
                    229:                return n;
                    230:        else {
                    231:                t = pwrreal0(n,e / 2);
                    232:                return e%2 ? t*t*n : t*t;
                    233:        }
                    234: }
                    235:
                    236: int cmpreal(a,b)
                    237: Real a,b;
                    238: {
                    239:        double t;
                    240:
                    241:        t = ToReal(a)-ToReal(b);
                    242:        return t>0.0 ? 1 : t<0.0?-1 : 0;
                    243: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>