473,666 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13305

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******@pacbe ll.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
25510
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
3341
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 images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task manager, that garbage collector doesn't actually do any collections unless the computer becomes low...
13
9835
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 convert it to a HEX value. I also need to find a way to determine if a number is a fraction in HEX and convert it back to DEC. (This would be from a disassembly, and we are learning this for troubleshooting purposes)
13
1649
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 pause(){ unsigned long i=0; while(i++<10000000); } int main(){ float i;
1
1466
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
25860
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
22
2072
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 fraction, a five bit value, 0-31. * */
17
2110
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
4263
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
8440
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8355
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
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7381
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 projectplanning, coding, testing, and deploymentwithout 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
6191
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
5662
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
4193
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
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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.