How I can solve C++ output negative numbers when using modulo? [duplicate] - c++

This question already has answers here:
Fastest way to get a positive modulo in C/C++
(9 answers)
How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers
(16 answers)
Closed 2 years ago.
In a code written by me, I have used both below functions to calculate mod of displayed negative number.
fmod(-10,11)
(-10, 11)
Though the correct answer is 1. It always displays the answer -10 in c++. How I can solve it?

From cppreference.com:
double fmod (double numer, double denom);
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less than y in magnitude.
In your case it is -10 - (-10)/11 * 11 = -10 - 0 * 11 = -10, which is correct for that implementation of fmod. If you need another answer, you should implement your own version, as modulo is defined in different ways for negative numbers.

Related

Is there an "inverted" trunc function in C or C++? [duplicate]

This question already has answers here:
c++ rounding of numbers away from zero
(4 answers)
Closed 3 years ago.
Is there a function in C or C ++ - similar to trunc - that rounds off negative numbers and rounds up positive numbers?
Like in this example:
-3.3 to -4 or 2.1 to 3
I could only find the "inverse" function trunc. But can hardly believe that this does not exist. Do I really have to first query the positivity via if and then round it up accordingly? I need this because I have the sign of the scalar product between two vectors. So either 1, -1 or 0.
First, you can use an inline conditional to either return the floor or the ceil. Here:
#include <math.h>
inline double InvertedTrunc(double Number) {
return Number < 0 ? floor(Number) : ceil(Number);
}
Another approach to achieve this functionality is just truncating the number, and increasing its absolute value by one. This will also work. Does not require math.h However, it is not recommended for large numbers because of overflow:
inline double InvertedTrunc(double Number) {
return (Number == (int)Number ? Number : ((int)Number)+(Number < 0 ? -1 : 1)); //casting to int truncates it
} //However, this option is susceptible to overflow, and it is not recommended for large numbers

How to write fraction instead of decimal in C++? [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 3 years ago.
I want to write 4/3 as a fraction in C++. I know that 4/3 is equal to 1.33333. But when I write 4/3 in the code it outputs it's quotient which is 1. Can anyobody tell me how to write this number as fraction?
The code:
double vol_sphere(double radiusS){
return (4/3) * pi * pow(radiusS, 3);
}
Because 4 and 3 are both integers, when you perform division of two integers, the result will be also integer, so 1.333333333 will be only 1.

Why does "double i = 1/12;" yields to i = 0? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 5 years ago.
I think the title says everything. I want to define a variable i as the fraction 1/12. However, i is 0.
double i = 1/12;
std::cout << i; // Output: 0
Or, more specific, I want to calculate a power of something:
im_ = std::pow((1 + i), (1/12)) - 1;
However, the compile evaluates (1/12) as 0 and thus the result is wrong.
Simple because 1/12 is evaluated as integer math, not floating point math.
1/12 becomes 0 because integer math does not take into account the decimal fractions.
To get the expected result you will need to write down the numbers as a floating point literal, like this: 1.0/12.0.
More details can be found here: Why can't I return a double from two ints being divided

Floating point rounding which rounds 0.5 to 0 [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
I did some search on the net. However, there is no solution for those case below.
if the value is less than or equal to 5, drop it and all the digits to the right of it.
if the value is greater than 5, increase by 1 the number to be rounded.
123.4561235 round to 123.456123.
123.4561236 round to 123.456124.
Is there any way to do so?
OK here you go.
Disclaimer: I haven't tested this thoroughly. Don't use this in the production code, and especially for money (you shouldn't use floating point for money at all).
double specialRound(double x, int precision) {
return std::round((static_cast<long long>(x * std::pow(10.0, precision + 1)) - 1) / 10.0 + 0.05)
/ std::pow(10.0, precision);
}
Demo

Math Calculation - float numbers [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 9 years ago.
I am doing the following
float years = (1/31536000) * 883102.00;
and I get years = 0000000
while the actual answer is 0.0.28
Any suggestion on what might be going wrong ?
You should instead do
float years = (1.0/31536000.0) * 883102.00;
or
float years = (1.0/31536000) * 883102.00;
might work as well. Your very first number is treated as integer.
Just do
float years = 883102.00/31536000;
This will save computation.
Since you are diving 1 by something and then multiplying.
Alternatively just set 1 to 1.00
1 is an int. The compiler then assumes that you are interested in working in ints and then 1/3153600 becomes 0. Just add a .0 to the 1 and your calc should work.
float years = (1.0/31536000) * 883102.00;
That first term is being cast as an int, and thus is being rounded to 0. Try this:
float years = (1.00 / 31536000.00) * 883102.00
(1/31536000) will yield 0 that multiplied by any number would be 0.
Make atleast one of numerator or denominator float (like 1.0 or 31536000.0)
883102.0 / 31536000 will do just what you want.
Because of the Integer-Division
(1/31536000)
the fractional digits get truncate and the result is "zero".
You have to add a dot:
(1.0/31536000.0) or (1./31536000.)