473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning integer portion of double

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.
--
Nov 14 '05 #1
2 1881
On Mon, 25 Apr 2005 23:27:19 GMT, Jon Ripley <ne**@jonripley.com>
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>>
Nov 14 '05 #2

"Jon Ripley" <ne**@jonripley.com> wrote in message
news:r1********************@fe2.news.blueyonder.co .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.
Nov 14 '05 #3

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

Similar topics

9
11890
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
3
2590
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
5
8236
by: da Vinci | last post by:
Hi Gents, This is what I am trying to do. Say you have a double or a float with the value 14.5624 for example. How could I take that variable and get the 14 into an integer variable and the...
3
19215
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
12
2625
by: Ivan Leo Puoti | last post by:
How can I get a function to return the .xxx part of a floating point number? Ivan.
3
1448
by: Nathan | last post by:
Hello, I have a function that reduces a fraction to lowest terms, storing the fraction in an integer array called Answer(). \\ Public Function Reduce(ByVal Numerator As Double, ByVal...
4
4333
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
8
2731
by: Michal Nazarewicz | last post by:
Hi, What does returning 0 from main() mean according to C89/C90 standard? I've found that in C99 it means successful termination (7.20.4.3p5) however as I'm editing book on C at Polish Wikibooks...
11
2042
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
7055
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,...
0
7103
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...
1
6758
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
7010
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...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
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...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.