| delorie.com/archives/browse.cgi | search |
| From: | "A. Sinan Unur" <asu1 AT c-o-r-n-e-l-l DOT edu> |
| Newsgroups: | comp.os.msdos.djgpp |
| Subject: | Re: C preprocessor not capable of floating point division ? |
| Date: | 21 Dec 2002 04:59:42 GMT |
| Organization: | Cornell University |
| Lines: | 77 |
| Sender: | asu1 AT cornell DOT invalid (on pool-141-149-208-78.syr.east.verizon.net) |
| Message-ID: | <Xns92EAF41569BACasu1cornelledu@132.236.56.8> |
| References: | <au0qd9$hkj$1 AT news DOT online DOT de> <Xns92EAF18D1776Casu1cornelledu AT 132 DOT 236 DOT 56 DOT 8> <au0s1n$hqn$1 AT news DOT online DOT de> |
| NNTP-Posting-Host: | pool-141-149-208-78.syr.east.verizon.net |
| X-Trace: | news01.cit.cornell.edu 1040446782 14579 141.149.208.78 (21 Dec 2002 04:59:42 GMT) |
| X-Complaints-To: | usenet AT news01 DOT cit DOT cornell DOT edu |
| NNTP-Posting-Date: | 21 Dec 2002 04:59:42 GMT |
| User-Agent: | Xnews/5.04.25 |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
| Reply-To: | djgpp AT delorie DOT com |
"Lars O. Hansen" <lars DOT o DOT hansen AT gmx DOT de> wrote in
news:au0s1n$hqn$1 AT news DOT online DOT de:
>> how do you propose to store 0.5 in an integer?
>>
>> > How can I achieve floating point division or similar having the
>> > same result by the C preprocessor for #define d values ?
>>
>> you'll have to clarify what you are trying to do.
>
> it should have been float a=2/sth.
>
> But DJ Delorie is right, the preprocessor or compiler which does
> expression evaluation beforehand "obeyes" the rules of x86 "integer
> division", so 4/8 is 0.
the preprocessor does simple text substitution:
C:\Dload\misc>cat fff.c
#define STH 8
float a = 2/STH;
C:\Dload\misc>gcc -E fff.c
# 1 "fff.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "c:/djgpp/lib/gcc-lib/djgpp/3.21/djgpp.ver" 1
# 1 "c:/djgpp/include/sys/version.h" 1 3
# 2 "c:/djgpp/lib/gcc-lib/djgpp/3.21/djgpp.ver" 2
# 2 "<command line>" 2
# 1 "fff.c"
float a = 2/8;
OTOH, the compiler, upon finding a constant expression, initializes a
with the value of the expression
C:\Dload\misc>gcc -S fff.c
C:\Dload\misc>cat fff.s
.file "fff.c"
.globl _a
.section .data
.p2align 2
_a:
.long 0
.ident "GCC: (GNU) 3.2.1"
if you change the code to:
C:\Dload\misc>cat fff.c
#define STH 8
float a = 2.0/STH;
then you'll get:
C:\Dload\misc>gcc -S fff.c
C:\Dload\misc>cat fff.s
.file "fff.c"
.globl _a
.section .data
.p2align 2
_a:
.long 1048576000
.ident "GCC: (GNU) 3.2.1"
where 1048576000 is the float representation of 0.5.
--
A. Sinan Unur
asu1 AT c-o-r-n-e-l-l DOT edu
Remove dashes for address
Spam bait: mailto:uce AT ftc DOT gov
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |