473,509 Members | 12,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modulus with double variables

Hello NG,

i need to calculate the modulus with double precisson typed variables.

this works well with integers:
w1 = w1 % 360;

but what do i have to do if i want to use w1 as double?

Thanks a lot!
Peter

May 29 '06 #1
5 92230
"Peter Pippinger" <pe*************@gmx.de> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Hello NG,

i need to calculate the modulus with double precisson typed variables.

this works well with integers:
w1 = w1 % 360;

but what do i have to do if i want to use w1 as double?


Well, you have the problem that floating point division does not have a
remainder, otherwise it wouldn't be floating point. But, there is still a
solution.

Take for an example
14.5 / 3.5;
The answer is:
4.1428571428571428571428571428571
So what would the remainder be defined as? This would be how many times 3.5
goes into 14.5 wholy. We see it goes 4 times plus a little bit. This is
easy to figure out. Take the answer as an integer. Multiply it by the
original divider. Then subtract this by the original divider.

float a = 14.5;
float b = 3.5;
int result = static_cast<int>( a / b );
float mod = a - static_cast<float>( result ) * b;


May 29 '06 #2
wow! That´s great. I have packed this in a function:

//
---------------------------------------------------------------------
// -- Modulus mit double Variablen
//
---------------------------------------------------------------------
double modulus(double a, double b)
{
int result = static_cast<int>( a / b );
return a - static_cast<double>( result ) * b;
}

Thanks a lot! you are great!

May 29 '06 #3

"Peter Pippinger" <pe*************@gmx.de> skrev i en meddelelse
news:11**********************@38g2000cwa.googlegro ups.com...
Hello NG,

i need to calculate the modulus with double precisson typed variables.

this works well with integers:
w1 = w1 % 360;

but what do i have to do if i want to use w1 as double?

Thanks a lot!
Peter


double fmod ( double x, double y ); // #include <cmath>

double r,x=5.3,y=2;
r = fmod(x,y); // r is not equal to 1.3

//eric
May 29 '06 #4
* Peter Pippinger:

i need to calculate the modulus with double precisson typed variables.

this works well with integers:
w1 = w1 % 360;

but what do i have to do if i want to use w1 as double?


Check out round and floor.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 29 '06 #5
Jim Langston <ta*******@rocketmail.com> wrote:
float a = 14.5;
float b = 3.5;
int result = static_cast<int>( a / b );
float mod = a - static_cast<float>( result ) * b;


I would worry that the static_cast would round to the
nearest integer rather than truncating towards negative
infinity.

Steve
May 29 '06 #6

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

Similar topics

6
7672
by: Chris Watson | last post by:
I'm trying to eliminate some warnings I'm getting -- probably not necessary but it's my final project: I have to use modulus so my variables are int. Is there some way to divide two results and...
8
3207
by: Philipp | last post by:
Hello, I would like to print (cout) the binary representation of a double. ie the values of the bytes 1-8. A typical output would be something like (in hex) ff af 12 d3 ab 9f 3c 00 Is there a...
11
2362
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
31
6575
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
2
2142
by: Daniel Molina Wegener | last post by:
Hello, How can I pad integral values on double variables with printf. I know that %.nf or %.ng sets the precision. But I want to pad the integral value with zeroes, like 001.023 or 032.012. ...
6
9297
by: Eric Lilja | last post by:
Hi, I need to implement a function that should take a double and split it into two integers. The decimalpart may be 0 but it may not be greater than 0.99 and not less than 0.01. In other words,...
1
1275
by: sharadbags | last post by:
example l=5*1e-7.8; whether this 7.8 we can put or not I m getting an error syntax error : missing ' : ' before 'constant' if this error is due to the power then how to overcome it ...
5
1785
by: RAMBO29 | last post by:
HI All; I am writing a software for a machine and it is working.But the out put comes as a string in this format: READOUT " -0.01 V, -0.04 A" /n Is it possible to get the two values i.e...
0
1003
by: robbie | last post by:
In the project upgrade from VS2002 to VS2005, all the double variables are initialize to 0.0000000. I checked the generated assembly instructions, the codes as below: double dMinVal = 0.01;...
0
7237
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
7137
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...
1
7074
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
7506
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...
0
5659
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,...
0
4734
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
3219
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
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.