473,811 Members | 3,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2718
On Tue, 10 Feb 2004 17:07:56 +0100 in comp.lang.c++, "Link"
<ju********@lil asystem.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.we st.earthlink.ne t...
On Tue, 10 Feb 2004 17:07:56 +0100 in comp.lang.c++, "Link"
<ju********@lil asystem.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********@com cast.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********@lil asystem.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********@lil asystem.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.********@com Acast.net> wrote in message news:E58Wb.2718 79$xy6.1382070@ attbi_s02...
"Link" <ju********@lil asystem.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.********@com Acast.net> wrote in message news:E58Wb.2718 79$xy6.1382070@ attbi_s02...
"Link" <ju********@lil asystem.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

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

Similar topics

5
1880
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 < float(Y) )?
14
2726
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 hurry to try and catch some floating point naughtiness, but was stumped in various places. Is there a fundamental obstacle to doing this? thanks, G.A.
16
2562
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 is this happening, and how do I get rid of it? (using Dev-C++
6
2303
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
2415
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; if(x == y) { printf("x can be represented precisely by a float\n"); }
11
2290
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 length of x is not known at compile time.
20
3150
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 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch
8
6162
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
6196
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
3
10674
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 with this and if there is a better solution. -jeff
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6882
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.