Zero-crossing Sign function [closed] - c++

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have written a Sign function and am wondering whether it is correct or not (Stupid question to ask, I know!) I'm just interested in knowing whether this is the best method to solve this particular task:
template<typename T>
T sign(T n)
{
if(n < 0) return -1;
if(n > 0) return 1;
return 0;
}
Would this give accurate enough results for large datasets? Can anyone see a problem, that I haven't come across that may arise when putting this into a real-life context?
Thanks

I would change return 0; to return n;. If n is NaN, sign should return NaN, not 0.

Related

how do i write a code in c++ to find the maximum of 3 integer no. using function overloading [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 4 days ago.
Improve this question
using concept of function overloading making a program to find maximum of 3 integer no.
I don't understand how do i show overload a function that is taking 3 integers only
You can use the fact that the maximum of three numbers is the bigger of 1 of the numbers and the maximum of the other two.
int maximum(int a,int b) { return /* ... */ }
int maximum(int a,int b, int c) { return /* ... */ }

Why doesn't this code use the C++ power function directly? Can someone help me with the power function in this code? [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
This problem is to output a single integer, the number of possible combinations calculated:
int power(int a, int n)
{
if (n == 0)
return 1;
// else
if (n % 2 == 0) {
int temp = power(a, n / 2);
return temp * temp;
}
// else
return a * power(a, n - 1);
}
This function uses a technique called exponentation by squaring.
It's a particularly efficient way of evaluating the power for integral type arguments. The standard C function uses floating point arguments, and the C standard doesn't require an exact result even if the floating point arguments represent whole numbers.
In C++ though you can probably rely on one of the overloads of std::pow that takes integral type arguments, and cast the result, subject to your making the necessary size checks. But again even the C++ standard does not require that the best possible result is returned (cf. std::sqrt under IEEE754), although one could reasonably regard a std::pow function that does not return the correct result for integral arguments to be defective.

How can I improve RSA d-key generation in c++? [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 7 years ago.
Improve this question
I'm writing a c++ implementation of RSA algorithm. It work but It takes hours to calcultate d key. Any help to make it work faster would be awesome.
unsigned __int64 calcolo_d(const unsigned __int64 eulero, const unsigned __int64 e) {
register unsigned __int64 d = 0;
while (!((e*d) % eulero == 1))
{
++d;
}
return d; }
What you are computing is the modular inverse of e mod eulero. This can be done efficiently via the extended euclidean algorithm.
There are many, many implementations out there to choose from.

Calculate the log base n with Shift left or Shift right [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a little problem.
Who knows how we can calculate the log
base n with Shift_L or Shift_R?
for example: for n=2 we had this solution:
int log(int n){
int res = 0;
while((n>>=1))
res++;
return res;
}
You don't seem to want the logarithm for a base b, but the largest integer n so that n <= log_b(x). If that's the case, the following function should serve your needs:
int intlog(double base, double x) {
return (int)(log(x) / log(base));
}
well this is rather a math problem instead of an actuall programming problem, if i understand your problem correctly:
log_2 (x) = log_a (x) / log_a (2) where a can be any base.
Therefore you could use the math.h's function log(double)
double res = log(x)/log(2);

number repeated twice in the variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int a = 101;
return 0;
}
Question : How do I know that the number (1) is repeated twice in the variable
If you look at the code, you will see that the number 101 is assigned to the variable a and that number has the digit 1 twice in its decimal representation. So direct inspection is the way to go. I wouldn't even write the code for such a trivial requirement.
Use modulus 10 and division 10 to find it. Rough idea is,
while( a > 0 )
{
if( a % 10 == 1 )count_one++;
a=a/10;
}