473,411 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

int, float

I have to convert an float number to int without using casting i= (int) f,
without printf and others. I can only use int, char and structs. Can anyone
help?

Thanks
Juerg
Jul 22 '05 #1
13 2684
On Tue, 10 Feb 2004 17:07:56 +0100 in comp.lang.c++, "Link"
<ju********@lilasystem.ch> was alleged to have written:
I have to convert an float number to int without using casting i= (int) f,
without printf and others. I can only use int, char and structs.


What is it about the standard built-in conversion from float to int
(what you call "casting") that makes it unsuitable for your application?
Anything else is doing it the hard way.

Jul 22 '05 #2
Link writes:
I have to convert an float number to int without using casting i= (int) f,
without printf and others. I can only use int, char and structs. Can anyone help?


I assume this is a student assignment. The first step is to find out how
your platform encodes a float. Look at the documentation that came with
your compiler. It is almost certain that an int is encoded as a two's
complement binary number and it's size will be in <limits.h>. Are you sure
that unions are forbidden? They might turn out to be handy, too.

Sounds like a fun and informative exercise.
Jul 22 '05 #3
Yes, I have it to do in the hard way - Its only a little box (not pc) and so
I can't use all libs (reduced memory, ...)
You know someting about working with exp, mantissa and so, only with int and
struct?

"David Harmon" <so****@netcom.com> schrieb im Newsbeitrag
news:40***************@news.west.earthlink.net...
On Tue, 10 Feb 2004 17:07:56 +0100 in comp.lang.c++, "Link"
<ju********@lilasystem.ch> was alleged to have written:
I have to convert an float number to int without using casting i= (int) f,without printf and others. I can only use int, char and structs.


What is it about the standard built-in conversion from float to int
(what you call "casting") that makes it unsuitable for your application?
Anything else is doing it the hard way.

Jul 22 '05 #4
Its a work on a little box with not much memory and I'm using borland turbo
compiler (TC)

typedef struct _MF {
unsigned long m : 23;
unsigned int e : 8;
unsigned int s : 1;
} MF;

union are ok.

You know more about working with mantissa, exponent and so?

"osmium" <r1********@comcast.net> schrieb im Newsbeitrag
news:c0*************@ID-179017.news.uni-berlin.de...
Link writes:
I have to convert an float number to int without using casting i= (int) f, without printf and others. I can only use int, char and structs. Can anyone
help?


I assume this is a student assignment. The first step is to find out how
your platform encodes a float. Look at the documentation that came with
your compiler. It is almost certain that an int is encoded as a two's
complement binary number and it's size will be in <limits.h>. Are you

sure that unions are forbidden? They might turn out to be handy, too.

Sounds like a fun and informative exercise.

Jul 22 '05 #5
"Link" <ju********@lilasystem.ch> wrote...
I have to convert an float number to int without using casting i= (int) f,
without printf and others. I can only use int, char and structs. Can anyone help?


What is it, a well-camouflaged homework? Most processors that
can work with floating point numbers directly can load them into
their register as FP and store as an int. Two instructions, no
printf necessary. If you are so bound to a particular "box" as
you claim, use assembly language. Or does your processor lack
any support for floating-point numbers?
Jul 22 '05 #6
Link wrote:
Its a work on a little box with not much memory and I'm using borland turbo
compiler (TC)

typedef struct _MF {
unsigned long m : 23;
unsigned int e : 8;
unsigned int s : 1;
} MF;

union are ok.

You know more about working with mantissa, exponent and so?


Well, you've virtually answered your question -

given that the floating point number is:

(s ? -1: 1 ) x m x 2^(e)

I think you can figure it out - however, be careful with 'e'. Is 'e'
really unsigned ? You also need to know what "m" is - is m really
normalized - meaning is the expression really :

(s ? -1: 1 ) x m x 2^(e-23)

Can you figure it out from there ?

Jul 22 '05 #7
On Tue, 10 Feb 2004 17:39:16 +0100 in comp.lang.c++, "Link"
<ju********@lilasystem.ch> was alleged to have written:
Its a work on a little box with not much memory and I'm using borland turbo
compiler (TC)


Use the Borland assembly source out (the -S option if I recall) and
look at what it generates for the cast. Then consider whether you still
think you can do better. If you can, the assembly source will probably
give some clue as to which way to go.

As for comp.lang.c++, we do try to keep to standard portable stuff
here, so twiddling mantissas and such will not be too popular.
See the welcome message posted twice per week in comp.lang.c++ or
available at http://www.slack.net/~shiva/welcome.txt

Jul 22 '05 #8

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:E58Wb.271879$xy6.1382070@attbi_s02...
"Link" <ju********@lilasystem.ch> wrote...
I have to convert an float number to int without using casting i= (int) f,
without printf and others. I can only use int, char and structs. Can

anyone
help?


What is it, a well-camouflaged homework? Most processors that
can work with floating point numbers directly can load them into
their register as FP and store as an int.


Too bad that doesn't include pentiums.

Jul 22 '05 #9
On Tue, 10 Feb 2004 14:21:37 -0500, "Ron Natalie" <ro*@sensor.com>
wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:E58Wb.271879$xy6.1382070@attbi_s02...
"Link" <ju********@lilasystem.ch> wrote...
> I have to convert an float number to int without using casting i= (int) f,
> without printf and others. I can only use int, char and structs. Can

anyone
> help?


What is it, a well-camouflaged homework? Most processors that
can work with floating point numbers directly can load them into
their register as FP and store as an int.


Too bad that doesn't include pentiums.


It doesn't? FILD/FIST don't work? Bit on the slow side, but they
always worked for me.....

Jul 22 '05 #10

"Andy Buchanan" <ne**@globbits.co.uk> wrote in message news:lq********************************@4ax.com...
>> What is it, a well-camouflaged homework? Most processors that
can work with floating point numbers directly can load them into
their register as FP and store as an int.


Too bad that doesn't include pentiums.


It doesn't? FILD/FIST don't work? Bit on the slow side, but they
always worked for me.....


FIST isn't slow. However, FIST won't in most cases convert between
float and int as the processor is almost certainly in round mode. Therefore
you have two options:

1. Modify the fcw to change the state to truncation, do the fist, then put it
back. This is EGREGIOUSLY SLOW.
2. Subtract .5 from the value before doing the fist. (This is actually pretty fast
but I don't know ANY compiler that does this).
Jul 22 '05 #11
Ron Natalie wrote:
FIST isn't slow. However, FIST won't in most cases convert
between float and int as the processor is almost certainly in
round mode. Therefore you have two options:

1. Modify the fcw to change the state to truncation, do the
fist, then put it back. This is EGREGIOUSLY SLOW.
2. Subtract .5 from the value before doing the fist. (This is
actually pretty fast but I don't know ANY compiler that does this).


The x87 round-to-nearest mode chooses the even value in case of a
tie, so 3.0 is "truncated" as 3.0 - 0.5 = 2.5 -> 2; probably not the
intended result.
Martin

--
Quidquid latine dictum sit, altum viditur.
Jul 22 '05 #12

"Martin Eisenberg" <ma*****************@PAMudo.edu> wrote in message news:10***************@ostenberg.wh.uni-

1. Modify the fcw to change the state to truncation, do the
fist, then put it back. This is EGREGIOUSLY SLOW.
2. Subtract .5 from the value before doing the fist. (This is
actually pretty fast but I don't know ANY compiler that does this).


The x87 round-to-nearest mode chooses the even value in case of a
tie, so 3.0 is "truncated" as 3.0 - 0.5 = 2.5 -> 2; probably not the
intended result.

Prescott by the way has a new FISTTP instructions that always truncates
(regardless of the FCW). I guess enough C programmers finally screamed.

Jul 22 '05 #13
On Wed, 11 Feb 2004 12:12:07 -0500, "Ron Natalie" <ro*@sensor.com>
wrote:

"Andy Buchanan" <ne**@globbits.co.uk> wrote in message news:lq********************************@4ax.com...
>> What is it, a well-camouflaged homework? Most processors that
>> can work with floating point numbers directly can load them into
>> their register as FP and store as an int.
>
>Too bad that doesn't include pentiums.
It doesn't? FILD/FIST don't work? Bit on the slow side, but they
always worked for me.....


FIST isn't slow.


If I'm doing thousands of them it starts to add up :-)
IIRC it's 6 cycles or so, that hurts when I've managed
to finese away just about every expensive calculation
in an inner loop. It's all relative I suppose.
However, FIST won't in most cases convert between
float and int as the processor is almost certainly in round mode. Therefore
you have two options:
1. Modify the fcw to change the state to truncation, do the fist, then put it
back. This is EGREGIOUSLY SLOW.
2. Subtract .5 from the value before doing the fist. (This is actually pretty fast
but I don't know ANY compiler that does this).


Well okay, you're right of course, but generally when I've had to hit
the assembler for performance reasons I've usually been quite happy to
accept this single bit error.

Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
11
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
8
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.