Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Returning integer portion of double

Reply
Thread Tools

Returning integer portion of double

 
 
Jon Ripley
Guest
Posts: n/a
 
      04-25-2005
Hi,

This function should return the integer portion of a float truncating
down to -infinity. Where int64(12.34) = 12 and int64(-12.34) = -13 but
int64(-12) = -12.

In reality the function is called with a pointer to a double.

void int64 ( double *num )
{
double tmp;

tmp=(long)*num;
if ((tmp < 0) && (*num != tmp))
*num=tmp-1;
else
*num=tmp;
}

When given any positive number it works, when given any non-integer
negative number it works. When given a negative integer it always
returns zero.

I'm stuck, I can't get it to return correct data in the last case.

This is one of many routines I am writing to manipulate the integer
portions of 64-bit floats.

Another:

double mod64(double *x, double *y)
{
return (double)((long)*x % (long)*y));
}

Just causes the calling routine to abort rather messily.

Please help!

TIA,
Jon R.
--
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-26-2005
On Mon, 25 Apr 2005 23:27:19 GMT, Jon Ripley <(E-Mail Removed)>
wrote:

>Hi,
>
>This function should return the integer portion of a float truncating
>down to -infinity. Where int64(12.34) = 12 and int64(-12.34) = -13 but
>int64(-12) = -12.
>
>In reality the function is called with a pointer to a double.
>
>void int64 ( double *num )
>{
> double tmp;
>
> tmp=(long)*num;
> if ((tmp < 0) && (*num != tmp))
> *num=tmp-1;
> else
> *num=tmp;
>}
>
>When given any positive number it works, when given any non-integer
>negative number it works. When given a negative integer it always
>returns zero.


Please provide a complete compilable program that demonstrates the
behavior you describe. I ran your code and got only the expected
results.

>
>I'm stuck, I can't get it to return correct data in the last case.
>
>This is one of many routines I am writing to manipulate the integer
>portions of 64-bit floats.
>
>Another:
>
>double mod64(double *x, double *y)
>{
> return (double)((long)*x % (long)*y));
>}
>
>Just causes the calling routine to abort rather messily.


The cast to double is superfluous. You have an extra right
parenthesis. Again, I ran your code and got the expected results.
You need to provide a compilable example that causes the problem.



<<Remove the del for email>>
 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      04-26-2005

"Jon Ripley" <(E-Mail Removed)> wrote in message
news:r1fbe.151396$(E-Mail Removed) .uk...
> Hi,
>
> This function should return the integer portion of a float truncating down
> to -infinity. Where int64(12.34) = 12 and int64(-12.34) = -13 but
> int64(-12) = -12.
>
> In reality the function is called with a pointer to a double.
>
> void int64 ( double *num )
> {
> double tmp;
>
> tmp=(long)*num;
> if ((tmp < 0) && (*num != tmp))
> *num=tmp-1;
> else
> *num=tmp;
> }
>
> When given any positive number it works, when given any non-integer
> negative number it works. When given a negative integer it always returns
> zero.
>
> I'm stuck, I can't get it to return correct data in the last case.
>
> This is one of many routines I am writing to manipulate the integer
> portions of 64-bit floats.
>
> Another:
>
> double mod64(double *x, double *y)
> {
> return (double)((long)*x % (long)*y));
> }
>
> Just causes the calling routine to abort rather messily.

What difference is there between the definition of your first function and
floor() ? Or between the second and fmod() ? The functions you have
written have undefined behavior in the case where the (long) cast overflows,
which is not at all unlikely for those C implementations where long has 31
bits plus sign.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Size of Integer , long Integer, Long double Suresh V C++ 5 07-05-2010 08:09 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Help: Double convert to Integer and Double.... da Vinci C++ 5 07-31-2004 12:35 AM
String question: Returning portion of string with words surrounding highlighted search term? Ken Fine ASP General 4 08-14-2003 06:04 AM



Advertisments