i am begginner in c++
below is my code ,I am using fmod(),in which the value of k should be 0.23 but it is giving it 0
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int k;
k=fmod(234.23,3);
cout<<"k="<<k<<endl;
return 0;
}
I am using codeblock.
Is it a compiler problem ?
You are assigning the result to a variable of type int, and int by its very nature can only represented integer numbers.
You need to use a floating-point type, such as float or double.
You must use float instead of int for k. An integer can only hold exact numbers, but no fractions. For this use, float or double.
C++ is a strongly typed language.
The result of fmod(234.23, 3) is 0.23, but you use an int to store it, so 0.23 is converted to a integer 0.
You should declare as float k;
Related
I am trying to do a division of :-
#include <bits/stdc++.h>
using namespace std;
int main(){
int A = -2147483648;
int B = -1;
int C = A/B;
// this is not working
cout<<C<<endl;
// nor this is working
cout<<A/B<<endl;
// But this is working
cout<<-2147483648/-1<<endl; // printing the result 2147483648;
}
I am confused why this happening. Please explain.
Assuming the int type is 32-bits and uses two's complement representation, the first two cases exhibit undefined behavior because both -2147483648 and -1 fit in a int but 2147483648 does not.
In the third case, the expression -2147483648/-1 contains the integer literal 2147483648 (before being negated), and has the first type in which the value can fit. In this case, that would be long int. The rest of the calculation keeps the type, so no undefined behavior occurs.
You can change the data type to long long.
long long A = -2147483648;
long long B = -1;
long long C = A/B;
If your you need fractional result, try 'double' instead of 'long long'.
(calculating the sum of long long numbers)
whenever I input a long long number the program returns always -1 or -2 anyone knows why?
#include <iostream>
#include <math.h>
#include <string>
#include<iomanip>
using namespace std;
int main()
{
int n,i;
long long x1,x2,s;
do{
cin>>n;
}while(n<1);
long long t1[n];
long long t2[n];
string ch1,ch2;
i=0;
do{
do{
scanf("%lld %lld", &x1, &x2);
ch1=to_string(x1);
ch2=to_string(x2);
}while((ch1.length()>pow(10,5)) || (ch2.length()>pow(10,5)));
t1[i]=x1;
t2[i]=x2;
i++;
}while(i<n);
for(i=0;i<n;i++){
s=t1[i]+t2[i];
cout<<s<<endl;
}
return 0;
}
can't find a solution please help! and thx in advance!
example input:
1
14444444444444444111115556
55656464684646464646647676
output
-2
//edit:
Is there a way to calculate the sum of 100000 :) decimal digits numbers like the ones in the example using standard libraries ?
Fundamental integral types such as long long have limited range that they can represent. If a calculation causes results in a larger result than can be represented, it will overflow. Signed integer overflow has undefined behaviour in C++.
It is possible to represent an infinite range of numbers (in theory, memory limits the practice) by using an array of integers to represent different parts of the number. This technique is called arbitrary precision arithmetic.
There is no implementation of arbitrary precision types in the C++ standard library, so either you must implement one yourself, or you can use a library implemented by others.
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
float f=static_cast<float>(5/2);
printf("%f",f);
return 0;
}
The answer is always 2.0.
I searched before asking but couldn't find the answer.
You just need to do
float f = 5.0f / 2;
In your code, 5 and 2 are ints, so in 5/2 the / operator is the integer division operator; the result will always be an integer, in this case, 2. Only then is it converted to float, giving 2.0.
You need to do floating point division, so at least one of the operands needs to be of floating point type. You could just write 5.0, without the f, but that would have type double by default, and the result will then be converted to float - a conversion that can cause issues (not here for these particular constant values, but still, it's better to use the right type in the first place).
This works on my compiler:
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
float f=(float)5/(float)2;
printf("%f",f);
return 0;
}
int main() {
float f = 5.0 / 2;
printf("%f", f);
return 0;
}
I am in need of writing UNIX/LINUX srand48 and drand48 functions in C. I am stuck with setting and using a seed value. I have two functions:
#include <math.h>
long int mfml_win_drandX0; //actual value used for generation
void srand48(long int seedval)
{
mfml_win_drandX0=seedval; //setting seed into mfml_win_drandX0
}
double drand48(void)
{
static const double a=0x273673163155,
c=0x13,
m=281474976710656;
/*EDIT: error was here*/
mfml_win_drandX0=fmod(a*mfml_win_drandX0+c,m);; //computing the next value
return mfml_win_drandX0/m;
}
But when using:
srand48(2) ;
for (int i=0;i<10;i++)
std::cout<<drand48()<<std::endl;
I get the same number everytime (mfml_win_drandX0) does not change. How to solve this issue?
You cannot guarantee that mfml_win_drandX0 will be in a range that representable by a long int, even if you choose long long for its type. AFAIK this is Undefined Behavior:
C++11 §5/4:
“If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.”
And I guess you should avoid defining m in this way because when you enter a literal that has an integral type, it is defined as an int. Using
m = 281474976710656LL;
is better, however this does not solve your main problem. Also, take care with
mfml_win_drandX0 = a * mfml_win_drandX0 + c;
Where you do a conversion from const double to long.
What do you expect to get though? There is nothing random in your code. a, c, m are all constant, you won't get different result util changing mfml_win_drandX0.
#include<iostream>
#include<cstdio>
#define M 1000000007
using namespace std;
long long int power(int a,int b)
{
if(b==0)
return 1;
else if(b==1)
return a;
else if(b%2==0)
return power((a*a)%M,b/2);
else
return (power((a*a)%M,b/2)*a)%M;
}
In this function when I pass a=2, b>31, it always returns 0.for b=31 i got 147483634.can you tell where the problem is?
or can you tell another method for calculating large powers of a number.
In (a*a)%M, a*a probably overflows before computing the remainder. And starting with 2, the overflow producing 0 doesn't surprise me. You need to work with a type able to represent (M-1)*(M-1), i.e. 1000000012000000036 while int is commonly limited to 2147483647. long long (standard in C since 99 and C++ since 11, common extension elsewhere) is guaranteed to work.
Under <cmath> there's pow(x,y) in which x is the base and y is the exponent. x^y.
Reference here
When a is an int, a*a uses int-sized arithmetic. Try a*(long long)a instead, to use wider arithmetic that won't overflow.