% versus FMOD for calculating modulus [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My console app looks like that.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
cin>>a>>b;
cout<<"% "<<a%b<<endl<<"fmod "<<fmod(a,b)<<endl;
system("pause");
return 0;
}
I'm newbie to C++ and I got 2 questions:
Writing this application on VS. Why do I need to include "stdafx.h"? Is there any requirement? What is this?
Is there any difference between fmod and % ? Getting exactly same results for them:
Thx in advance..

Writing this application on VS. Why do I need to include "stdafx.h"? Is there any requirement? What is this?
Because the default project setting says you need precompiled header (See this).
You can disable this manually. Select Not Using Precompiled Headers as shown in the image below:
Is there any difference between fmod and % ? Getting exactly same results for them:
Yes. % cannot operate on floating-pointer numbers, while fmod can. f in fmod indicates floating-point.
Try this:
float a, b;
std::cin>>a>>b;
std::cout << (a%b) << std::endl; //it will give compilation error.

fmod( a , b ) will cast the int variables a and b to floats when the parameters are passed. Depending on the type of a and b (for instance, if you use std::uint64_t) you might lose precision during the cast and get something incorrect returned. The return type will also be a float and will need to be cast again if you're using the function with int types. You should stick to % for int types. Using fmod is less efficient.

Related

How to use modulus with a global variable and namespace std? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Yes I know using namespace std is bad practice, but I have already written a majority of the code with this declaration in place, and I don't think I have the time to go back and modify it.
The reason for the global variable is that I'm using multiple threads which need access to this variable and need to modify it.
My question is, I have int remainder = 0; declared globally, and within my master thread I call remainder = 13 % 5; for example.
This gives me an error saying 'int remainder' redeclared as a different kind of symbol and I've read that the reason is that using namespace std overrides the std::modulus operator, if I understood that correctly.
What other methods can I use to perform this function, keeping using namespace std and remainder as a global variable?
#include<iostream>
#include<cmath>
using namespace std;
int remainder = 0;
void testing();
int main(){
testing();
cout << remainder << endl;
return 0;
}
void testing(){
remainder = 13 % 5;
}
The problem is your global variable name conflicts with std::remainder from the standard library. Example on Compiler Explorer.
The problem with using namespace std; is that it brings so many symbols into the global namespace that this error is almost inevitable. It's a bad practice for anything but the simplest toy programs.
The conflict is with std::remainder, not with %. The variable name you've chosen conflicts with a function in the std namespace. You already know using namespace std; is bad, so I'll spare you.
Options:
Lose the using statement.
Rename the remainder variable.
Put the remainder variable in its own namespace, and explicitly refer to it through that namespace.

Is it possible to create new type variable without struct? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is what i got but when i check type it displays "struct MyVarName"
#include <iostream>
#include <typeinfo>
typedef struct { char text[15];} MyVarName;
int g = 0;
int main(void) {
MyVarName a = { "super" };
std::cout << typeid(a).name() << '\n';
std::cout << typeid(g).name() << '\n';
return 0;
}
Is it possible to define your own variable type and then use it as any other variable?
Print, assign new value.. etc.
The result of std::type_info::name() is implementation-defined and could be anything, including a mangled name, the empty string, or a recipe for lasagna.
For example, GCC gives:
9MyVarName
i
Apparently yours is taking a C-like approach and calling it struct MyVarName, which is what you'd have to write to reference the type in C.
Simply don't rely on it for this kind of thing. C++ does not pretend to have meaningful reflection.
Besides that, there is nothing wrong with your code. You did define a new type. There are ways to introduce new types that don't require the creation of a class type (i.e. with struct or class), but these all involve aliasing existing types with typedef or using, and are thus limited. They won't allow you to create a complex type like std::string.
in C:
typedef char newtype[20];
void foo(void)
{
newtype a;
}

Need help whit c++ pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm studing about c++ semantics and syntax, I really don't know what is the problem with this code, it compile but stop working. I will apreciate your help, thanks.
#include <iostream>
#include <string.h>
using namespace std;
char* func(char* M)
{
int initval = 2;
char *x= new char[10];
x="idea";
strcpy(x, M+initval);
return x;
}
int main()
{
char* x;
char s[10]= "alguna";
x= func(s);
cout << *x << endl;
return 0;
}
Before this is closed, the x="idea"; is where your problem lies. You throw away your buffer and point it to a constant value, then try to assign to it, which almost always is illegal (should always be illegal, but apparently it is compiling for you...).

sqrt function doesn't evaluate correct value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm newbie in C++ and I'm trying to evaluate a square root. I've written the following:
#include <math.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
double a;
a=sqrt(2.0);
printf("Square root from 2 is %d\n",a);
return 0;
}
But output is Square root from 2 is 1719614413. I really don't understood this. Please explain me.
You are using C (which mostly compiles as C++ as well), not C++ and you made a mistake doing so. You can either learn C or use C++, where it's harder to make those mistakes:
#include <cmath>
#include <iostream>
int main()
{
double a = std::sqrt(2.0);
std::cout << "Square root from 2 is " << a << std::endl;
return 0;
}
Additional explanation:
printf("Square root from 2 is %d\n",a);
This prints a string and expects an integer (%d) to be passed. You passed a double, wich comes out as garbage. You can use (%lf) for doubles.
If you still want to stick to our example, please consider using %f rather than %d:

optimizing code performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i am kind of new in c++. I was trying to optimize my code. My code includes two for loops with a if block inside the second loop. first loop will iterate for 10^14 times and inner for loop will iterate for 10^4 times. My code is as folows
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<stdio.h>
#include<fstream>
using namespace std;
#include<math.h>
#include<thread>
using namespace std;
signed long long run,i,j;
int main()
{
run=0;
for (i=0;i<100000000000000;i++)
{
for (j=0;j<10000;j++)
{
run=run+1;
}
}
cout<<run<<"\n";
}
time it is takling to complete is around 1 day. So I was using thread in my code to make it first. But it is showing to include -std=c++0x. So where to include this?
Is there anyone who would like to help me out?
I'd optimise it as follows, but good compilers might do that anyway:
#include<iostream>
signed long long run,i,j;
int main()
{
i = 100000000000000;
j = 10000;
run = i * j;
std::cout << run << '\n';
}
-std=c++0x is a compiling option, and it tells the compiler you are using C++11 standard.
You might want to take a look at a link like this one http://www.cs.cf.ac.uk/Dave/C/node3.html regarding compiling.