473,606 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ 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 1891
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******** ************@fe 2.news.blueyond er.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
11902
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 up to which size m would you expect vector<double> returns_by_value() {
3
2604
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 similar behavior. My class looks like this: #ifndef long_int_H #define long_int_H #include <string> using namespace std; typedef valarray<complex<double> > VCD;
5
8249
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 .5624 into a double or float? The reason is because of converting decimal locations into DMS. Math is simple....
3
19222
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
12
2643
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
1464
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 Denominator As Double) As Integer Dim Answer(2) As Integer ....
4
4353
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 Architecture. The Stored Procedures are used through TableAdaptors, which in turn are used by Class Files. I wish to be able to return this new ID value using the Stored
8
2736
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 I'd like to know what C89/C90 says about it - quotation from C89 or C90 would be nice. Moreover, in C99 main() function is somewhat special because lack of return statement is equivalent with returning zero (5.1.2.2.3p1). Is it also the case...
11
2059
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 added and Divided by the number of ints to return the average, a float. Can someone see why I get for example: if the total is 227 then divided by 10 I get a return of 22.00000 instead of the correct answer 22.7.
0
8036
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
7978
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
8461
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
6796
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5987
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5470
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();...
0
3948
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2454
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

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.