Some newbie casting issues



 DEVELOP > C > Some newbie casting issues

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > C
User: "SabaUd"
Date: 01 Dec 2007 05:39:50 AM
Object: Some newbie casting issues
Hello,
I wrote a short program that needs to calculate capacity of stack.
i have 2 ints and im trying to do so.
my code:
Stack* selectedStack = ...;
double fActual = (double)selectedStack->nActualSize;
double fAllocated = (double)selectedStack->nAllocatedSize;
double fCapacity = (fActual / fAllocated) * 100.0;
printf("%d ", (int)ceil(fCapacity));
Now - this one works, but it's disgusting code...
I'm sure there's a more elegant way to execute it...
any ideas?
.

User: "Peter Nilsson"

Title: Re: Some newbie casting issues 01 Dec 2007 10:27:31 PM
SabaUd <Saba...@gmail.com> wrote:

I wrote a short program that needs to calculate capacity
of stack.
i have 2 ints and im trying to do so.
my code:

Stack* selectedStack = ...;
double fActual = (double)selectedStack->nActualSize;
double fAllocated = (double)selectedStack->nAllocatedSize;
double fCapacity = (fActual / fAllocated) * 100.0;
printf("%d ", (int)ceil(fCapacity));

Now - this one works, but it's disgusting code...

Assuming nXXXXX are indeed integers...
printf("%3.0f%%\n", 100.0 * selectedStack->nActualSize
/ selectedStack->nAllocatedSize);
--
Peter
.

User: "Minimiscience"

Title: Re: Some newbie casting issues 01 Dec 2007 09:53:17 PM
SabaUd <SabaUdi@gmail.com> writes:

Hello,
I wrote a short program that needs to calculate capacity of stack.
i have 2 ints and im trying to do so.
my code:

Stack* selectedStack = ...;
double fActual = (double)selectedStack->nActualSize;
double fAllocated = (double)selectedStack->nAllocatedSize;

These two casts aren't actually needed, unless you're using a very strict
compiler or you're compiling this as C++ code.

double fCapacity = (fActual / fAllocated) * 100.0;
printf("%d ", (int)ceil(fCapacity));

This cast, however, is needed so that printf() is passed the right type of
value.
--
You can't write me, I :q!
.

User: "santosh"

Title: Re: Some newbie casting issues 01 Dec 2007 07:28:13 AM
SabaUd wrote:

Hello,
I wrote a short program that needs to calculate capacity of stack.
i have 2 ints and im trying to do so.
my code:

Stack* selectedStack = ...;
double fActual = (double)selectedStack->nActualSize;
double fAllocated = (double)selectedStack->nAllocatedSize;
double fCapacity = (fActual / fAllocated) * 100.0;
printf("%d ", (int)ceil(fCapacity));

Now - this one works, but it's disgusting code...

I'm sure there's a more elegant way to execute it...

any ideas?

Yes. Provide separate functions that take a Stack * argument and return
details (like capacity, size etc.) about it. And consider using size_t
as the object type for returning size and capacity related values,
since that is the natural type for such purposes in Standard C.
.

User: "SabaUd"

Title: Re: Some newbie casting issues 01 Dec 2007 05:52:54 AM
oh, another one I forgot:
I want to print "30%"...
how do I print '%'?
.
User: "Simon Gerber"

Title: Re: Some newbie casting issues 01 Dec 2007 06:02:35 AM
SabaUd @ Sat, 01 Dec 2007 03:52:54 -0800:

how do I print '%'?

by typing %%
--
Simon Gerber
simugerber (at) student (dot) ethz (dot) ch
.

User: "Richard Heathfield"

Title: Re: Some newbie casting issues 01 Dec 2007 06:23:18 AM
SabaUd said:

oh, another one I forgot:
I want to print "30%"...
how do I print '%'?

See page 13 of "The C Programming Language", 2nd edition, by Kernighan and
Ritchie.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER