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

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

Related

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

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.

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

Why do I have to add a decimal to get this math correct in C++ [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 4 years ago.
I was calculating the volume of a sphere and after tons of research I found that I cannot use:
float sphereRadius = 2.33;
float volSphere = 0;
volSphere = (4/3) * (M_PI) * std::pow(sphereRadius, 3);
But must add the 3.0 instead to get the right answer.
volSphere = (4/3.0) * (M_PI) * std::pow(sphereRadius, 3);
Why must a decimal be added to get the correct calculation?
(4/3) is one integer divided by another integer, which results in another integer. An integer can't be 1.33 or anything like that, so it gets truncated to 1. With the decimal, you're telling it to be a double instead, and dividing an integer by a double results in a double, which supports fractions.

Integer precision [duplicate]

This question already has an answer here:
Fibonacci numbers becoming negative after a certain term
(1 answer)
Closed 5 years ago.
I was solving Project Euler problems using Fortran; the problem is to create a Fibonacci sequence and find the sum of all even numbers that come under 4 million. Here's what I wrote
implicit none
integer*4::a(1:4000000),sum
integer*4::i,maxc
maxc = 3999999
a(1) = 1
a(2) = 2
do i = 3,maxc,1
a(i) = a(i-1) + a(i-2)
end do
sum = 0
do i = 1,maxc
if (mod(a(i),2)==0) then
sum = sum + a(i)
end if
end do
print*,sum
end
The output is -1833689714
Any idea what went wrong?
Due to the size of the integer kind you chose, there is a limit to the numbers you can represent.
In your code it is 2147483647 (with gfortran, obtained by print *,huge(sum)).
It can be shown that this limit is exceeded for i=59 in your implementation.
Then, you get an integer overflow, and the value becomes negative.
Simply using a floating point representation for the sum, i.e.
real :: sum
Does the trick.

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.)