473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is the fastest way of getting the fraction part of a floating point?

ME
What is the fastest way of getting the fraction part of a floating point
number?

So, if I have a float 10.3 I want to get a float with value 0.3 fast.

Thanks


Feb 2 '06 #1
5 13275

ME wrote:
What is the fastest way of getting the fraction part of a floating point
number?

So, if I have a float 10.3 I want to get a float with value 0.3 fast.

Thanks


x - static_cast<int>(x);

Feb 2 '06 #2
ME wrote:
What is the fastest way of getting the fraction part of a floating point
number?

So, if I have a float 10.3 I want to get a float with value 0.3 fast.


Look up modf().

--
Later,
Jerry.

Feb 2 '06 #3
Tom
On Thu, 2 Feb 2006 20:13:09 +0100, "ME" <me@home.com> wrote:
What is the fastest way of getting the fraction part of a floating point
number?

So, if I have a float 10.3 I want to get a float with value 0.3 fast.

Thanks

From MS help. Convert to C++ style if you like.
Use 1.0 as the "y" number to get the fractional part.

double x = div(10.3, 1.0);
// result: x = 0.3;

/* DIV.C: This example takes two integers as command-line
* arguments and displays the results of the integer
* division. This program accepts two arguments on the
* command line following the program name, then calls
* div to divide the first argument by the second.
* Finally, it prints the structure members quot and rem.
*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

void main( int argc, char *argv[] )
{
int x,y;
div_t div_result;

x = atoi( argv[1] );
y = atoi( argv[2] );

printf( "x is %d, y is %d\n", x, y );
div_result = div( x, y );
printf( "The quotient is %d, and the remainder is %d\n",
div_result.quot, div_result.rem );
}
Output

x is 876, y is 13
The quotient is 67, and the remainder is 5


Feb 2 '06 #4
JE
Tom wrote:
On Thu, 2 Feb 2006 20:13:09 +0100, "ME" <me@home.com> wrote:
What is the fastest way of getting the fraction part of a floating point
number?

So, if I have a float 10.3 I want to get a float with value 0.3 fast.

Thanks

From MS help. Convert to C++ style if you like.
Use 1.0 as the "y" number to get the fractional part.

double x = div(10.3, 1.0);
// result: x = 0.3;


< snip >

That's not going to work. First of all, div returns a div_t or ldiv_t
struct. Second of all, div takes two ints or two longs as arguments
(double is trouble!). In your case, the remainder of the div_t return
div_t::rem would be 0...

Feb 2 '06 #5
Tom
On 2 Feb 2006 13:08:33 -0800, "JE" <je******@pacbell.net> wrote:
Tom wrote:
On Thu, 2 Feb 2006 20:13:09 +0100, "ME" <me@home.com> wrote:
>What is the fastest way of getting the fraction part of a floating point
>number?
>
>So, if I have a float 10.3 I want to get a float with value 0.3 fast.
>
>Thanks
>
>
>

From MS help. Convert to C++ style if you like.
Use 1.0 as the "y" number to get the fractional part.

double x = div(10.3, 1.0);
// result: x = 0.3;


< snip >

That's not going to work. First of all, div returns a div_t or ldiv_t
struct. Second of all, div takes two ints or two longs as arguments
(double is trouble!). In your case, the remainder of the div_t return
div_t::rem would be 0...


Ouch. I was Totally Wrong !!

Modf is the ticket. I think I need a nap.
Feb 2 '06 #6

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

Similar topics

15
by: mjs7231 | last post by:
I am trying to record how long an operation takes, but can't seem to find a function that will allow me to record the timestamp in milliseconds, maybe I am looking in the wrong place?
28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
13
by: puzzlecracker | last post by:
If you run this program, it will give very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call to execute 'pause'); #include<stdio.h> //void...
1
by: ME | last post by:
What is the fastest way of getting the fraction part of a floating point number? So, if I have a float 10.3 I want to get a float with value 0.3 fast. Thanks
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
22
by: dos.fishing | last post by:
Hello, I'm writing a function that should do the following: /** * Calculate and return fraction of valueA where max fractions is 31. * param valueA A five bit value, 0-31. * param valueB The...
17
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
Hello , Does someone knows a simple way of how to get the nr of fraction digits ? example : 1.23 would give a result of 2 1.234 would give a result of 3 Yes
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
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,...
0
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...

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.