Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
ONOTech

(C++) Floating Point Remainders?

Recommended Posts

Posted · Original PosterOP

Does anyone know if there is a floating point modulus operator (%) for C++? The regular operator only works with integers, but I need it to output and store a decimal remainder. Not quite sure how to do that.

 

Code (not sure why you need it, but oh well):

#include <iostream>#include <string> using namespace std; int main () {     string startPlace, endPlace;    double distance; // only two test output    double timeOne, timeTwo;    double speedOne, speedTwo;      cin >> distance;    cin >> timeOne;    cin >> timeTwo;    cin >> speedOne;    cin >> speedTwo;     double totalDistance = distance + speedTwo*(timeTwo - timeOne);    double totalSpeed = speedOne + speedTwo;     double time = totalDistance / totalSpeed;    double remainder = totalDistance % totalSpeed; //error is here, only works with ints    cout << time; //must output in hr:min min format    cout << remainder;      return 0;}/** FIND A WAY TO NOT PRESS ENTER AFTER EVERY INPUT **/
Link to post
Share on other sites

% can be rewritten as x-(x/y)*y (I think this is the right equation to replace %)


RIG: I7-4790k @ 4.5GHz | MSI Z97S SLI Plus | 12GB Geil Dragon RAM 1333MHz | Gigabyte G1 Gaming GTX 970 (1550MHz core/7800MHz memory) @ +18mV(Maxed out at 1650/7800 so far) | Corsair RM750 | Samsung 840 EVO 120GB, 1TB Seagate Barracuda | Fractal Design Arc Midi R2 (Closed) | Sound Blaster Z                                                                                                                        Getting: Noctua NH-D15 | Possible 250GB Samsung 850 Evo                                                                                        Need a console killer that actually shits on every console? Here you go (No MIR/Promo)

This is why you should not get an FX CPU for ANY scenario other than rendering on a budget http://linustechtips.com/main/topic/286142-fx-8350-r9-290-psu-requirements/?p=3892901 http://linustechtips.com/main/topic/266481-an-issue-with-people-bashing-the-fx-cpus/?p=3620861

Link to post
Share on other sites
Posted · Original PosterOP

% can be rewritten as x-(x/y)*y (I think this is the right equation to replace %)

I remember someone saying that somewhere. Thanks, I'll try it out.

Link to post
Share on other sites

I remember someone saying that somewhere. Thanks, I'll try it out.

Test it with standard integer values first like 7%4 to make sure it's working and whether I was actually right


RIG: I7-4790k @ 4.5GHz | MSI Z97S SLI Plus | 12GB Geil Dragon RAM 1333MHz | Gigabyte G1 Gaming GTX 970 (1550MHz core/7800MHz memory) @ +18mV(Maxed out at 1650/7800 so far) | Corsair RM750 | Samsung 840 EVO 120GB, 1TB Seagate Barracuda | Fractal Design Arc Midi R2 (Closed) | Sound Blaster Z                                                                                                                        Getting: Noctua NH-D15 | Possible 250GB Samsung 850 Evo                                                                                        Need a console killer that actually shits on every console? Here you go (No MIR/Promo)

This is why you should not get an FX CPU for ANY scenario other than rendering on a budget http://linustechtips.com/main/topic/286142-fx-8350-r9-290-psu-requirements/?p=3892901 http://linustechtips.com/main/topic/266481-an-issue-with-people-bashing-the-fx-cpus/?p=3620861

Link to post
Share on other sites
Posted · Original PosterOP

Test it with standard integer values first like 7%4 to make sure it's working and whether I was actually right

Tried it here, but I got no output. 

double test = 10-(10/4) * 4;    cout << test;
Link to post
Share on other sites

 

Tried it here, but I got no output. 

double test = 10-(10/4) * 4;    cout << test;

Then I don't know anymore how to rewrite %, You'll have to find someone else I'm sorry Q_Q


RIG: I7-4790k @ 4.5GHz | MSI Z97S SLI Plus | 12GB Geil Dragon RAM 1333MHz | Gigabyte G1 Gaming GTX 970 (1550MHz core/7800MHz memory) @ +18mV(Maxed out at 1650/7800 so far) | Corsair RM750 | Samsung 840 EVO 120GB, 1TB Seagate Barracuda | Fractal Design Arc Midi R2 (Closed) | Sound Blaster Z                                                                                                                        Getting: Noctua NH-D15 | Possible 250GB Samsung 850 Evo                                                                                        Need a console killer that actually shits on every console? Here you go (No MIR/Promo)

This is why you should not get an FX CPU for ANY scenario other than rendering on a budget http://linustechtips.com/main/topic/286142-fx-8350-r9-290-psu-requirements/?p=3892901 http://linustechtips.com/main/topic/266481-an-issue-with-people-bashing-the-fx-cpus/?p=3620861

Link to post
Share on other sites
Posted · Original PosterOP

Then I don't know anymore how to rewrite %, You'll have to find someone else I'm sorry Q_Q

No worries. Thanks regardless :)

Link to post
Share on other sites
double test = 10-(10/4) * 4;    cout << test;

Not sure why it wouldn't output anything. But, I'm not sure if C++ acts this way but there are some languages that do. That is (10/4) would equal 2, not 2.5. The correct way would be to write (10.0/4.0), at least one must be written with a decimal point or you can cast a number so it looks like ((float) 10/4). 

Link to post
Share on other sites
Posted · Original PosterOP
double test = 10-(10/4) * 4;    cout << test;

Not sure why it wouldn't output anything. But, I'm not sure if C++ acts this way but there are some languages that do. That is (10/4) would equal 2, not 2.5. The correct way would be to write (10.0/4.0), at least one must be written with a decimal point or you can cast a number so it looks like ((float) 10/4). 

 

It's my fault that it didn't output. I didn't put it in the right order in my code.

 

Added the decimal and now it just outputs 0. Without the decimals it outputs 2 as expected.

Link to post
Share on other sites

 

Tried it here, but I got no output. 

double test = 10-(10/4) * 4;    cout << test;

To say it this can't even work because what you are doing is dividng 10/4 thats the first thing what happens because it is in brackets so that equals to 2.5, then you would multiply it with for because dot before line, so that would equal to 10 then again and after that you do 10-10 and that equals to 0.

 

 

The function % just gives you the residual of a division and that only works with int, because a double or a float never has any kind of residual, because they will jsut keep going with the numbers after the comma/dot.  

 

 

So know a question what I have do you want to display with the remainder how much time is left till the timer goes to 0 or what do you want with that. Could be I can help you if you clarify that for me abit more pls

Link to post
Share on other sites
Posted · Original PosterOP

To say it this can't even work because what you are doing is dividng 10/4 thats the first thing what happens because it is in brackets so that equals to 2.5, then you would multiply it with for because dot before line, so that would equal to 10 then again and after that you do 10-10 and that equals to 0.

 

 

The function % just gives you the residual of a division and that only works with int, because a double or a float never has any kind of residual, because they will jsut keep going with the numbers after the comma/dot.  

 

 

So know a question what I have do you want to display with the remainder how much time is left till the timer goes to 0 or what do you want with that. Could be I can help you if you clarify that for me abit more pls

I understand your first two paragraphs, but I don't get the last sentence. 

 

Right now if I divide my two numbers I get 5.6817. I want the program to isolate the remainder (.6817) because I need to use that for something else.

Link to post
Share on other sites

I understand your first two paragraphs, but I don't get the last sentence. 

 

Right now if I divide my two numbers I get 5.6817. I want the program to isolate the remainder (.6817) because I need to use that for something else.

The best thing to get the things after the comma is what you do:

 //the number you got from somewhere as double/floatdouble number;    //now you convert your double/float number into the integer, so you get the number withoutthe stuff behind the . so 5 instead of 5.6817, then you convert it back to a double/float,so you can use it later in a substraktionint inumber = (int)number;           //you need to do this so it cuts off the decimal(this is no explicit converting, its implizit just to let you know)double dnumber = Convert.ToDouble//now you make a new double/float so you get the stuff after the .double newnumber = number - dnumber;Here is a example(just put it in a new project and you see what I mean):            Console.Write("Type in a number: ");            double number = Convert.ToDouble(Console.ReadLine());            int inumber = (int)number;            double dnumber = Convert.ToDouble(inumber);            double newnumber = number - dnumber;            Console.Write(newnumber);            Console.ReadKey();

So hopefully it helps you. If you have still any problems understanding just let me know and I try to explain it better.

Link to post
Share on other sites

I understand your first two paragraphs, but I don't get the last sentence. 

 

Right now if I divide my two numbers I get 5.6817. I want the program to isolate the remainder (.6817) because I need to use that for something else.

 

hmm, well there seems to be a bit of confusion, so hopefully this will help.

 

I will be using 15 / 1.1 (Y / X) as my example 13.6363636...., the .6363... is just the numbers after the decimal point, and shouldn't be considered the remainder [i am calling the remainder R].  The numbers after the decimal place is actually R / X (0.7/1.1).

 

The remainder for floating points is a bit different (depends on how you define mod, which will differ the results, but in general they are consistent).  How the remainder is usually is as follows Y%X = R  where Y = X*d + R, where 0 <= R < |x|...in this case d is the largest integer that fits.  So in the example the equation looks like this

15 = 1.1 * d + R.  Which d = 13.  This solves to R = 0.7

 

Anyways don't want to go too much into the math.

 

For your purposes if you actually want the remainder use fmod, it will give you the result just like % would.

If you want the numbers after the decimal point, you have a few options.  The easiest (but less precise method) would be simply doing this

result = fmod(dividend, divisor) / divisor;

Another way in C++ would be

//This does have it's faults but at the moment I think it would be too much to explaindouble getDecimalPoints(double number) {    return number - ((long) number));}

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×