TIP: Click on subject to list as thread! ANSI
echo: cis.languages
to: ALL
from: Robert DeBolt 76417,2225
date: 1991-03-23 00:04:05
subject: #C Problem

#: 9902 S3/Languages
    23-Mar-91  00:04:05
Sb: #C Problem
Fm: Robert DeBolt 76417,2225
To: ALL

Hi!

I  have been tinkering with an experimental interpreter (written
in  C) of an object-oriented "Forth-like" language I call  "XX".
It  now  only  handles longs and doubles.  In checking  out  the
arithmetic operators, I found the following anomaly:

                -100 / -5 = -20   ???

Here is a sample program to prove my point:

                    /* Module:  testlong.c */
                    #include 

                    main()
                    {
                        long x,y,q;

                        x = -100L;
                        y =   -5L;
                        q = x/y;
                        pflinit();
                        printf("x/y=%ld/%ld=%ld\n",x,y,q);
                    }

The printed result is:

                        x/y=-100/-5=-20

Is  this a compiler bug, or am I missing something about integer
division?

What would be the best solution to work arounr it?

                    if (x<0L && y<0L)
                         q = -x/y;
                    else q =  x/y;

Maybe?


Thanks, Bob.
list
type

There is 1 Reply.

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.