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 6 years ago.
Improve this question
My understanding is that NaN (Not a Number) is essentaly a constant that is returned from a mathematical function to indicate something went wrong or the calculation is invalid. So it makes sense that their are functions to check if a number is NaN or better yet, use the CERT Coding Standard to do error checking for mathematical errors ( https://www.securecoding.cert.org/confluence/display/c/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions ).
My question is this; why does std::nan() exist? Why would you ever want to take a valid number/string/value and convert it to NaN? (Refrence: http://en.cppreference.com/w/cpp/numeric/math/nan )
NaN is often used to indicate a null or missing value, especially in data analyisis and data science. So it is common for an application to initialize values to nan, in order to track whether a value has been provided or not without the overhead of using optional<T>-like structures.
Secondarily, it common to create custom math functions that you want to return nan for certain inputs. So it's more than just for completeness.
Suppose you want to implement std::acos. How would you return nan in case of invalid input (|arg| < 1)? It should be possible to implement such functions in C++. Beside that fact, that you may need to write a function which is not provided by STL, one of distinctive charts of C++ is that it's standard library can be written on C++.
IEEE 754 systematically introduced the use of NaN to represented numbers whose definitions could otherwise not be represented on computers.
You'll often see this for 0/0, ±inf / ±inf, 0 * ±inf, etc.
Related
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 4 months ago.
Improve this question
during my home task I had to create calculator that includes power function for rational numbers. At that time I wanted to create power function for complex and irrational numbers(something like modern calculator) using only pure c++. But unfortunately I could not find way even to represent complex and irrational numbers. So I'm stuck here.
Hope you can help me))
Edit: Apparently, you can use std::complex to try and solve your problem. Credit to #Pete Becker.
I'm just giving a suggestion here for complex numbers.
First, you can make a struct, where one member stores the constant of the real part, and one member stores the constant beside i in the imaginary part. You can store these as doubles.
i.e.
struct complex_num{
double real_part;
double complex_part;
}
Then, to compute a number raised to the complex number: we can do:
k^(a+bi) = k^a * k^bi
To calculate k^a, you can just use the built-in pow function.
To calculate k^bi, we need to use some more maths.
As you might know, k^bi can be written as (e^ln(k))^bi.
This can be rearranged to get (e^bi*ln(k))
e^((ln(k)*b)i) can be rewritten as cos(ln(k)*b) + i sin(ln(k)*b), which we can call q.
Hence: k^(a+bi) can be rewritten as pow(k,a) * q. This result can be stored in a variable of the type complex_num. I think to do the maths for this, all the functions you need are probably in the builtin maths library for c++.
I haven't got visual studio installed on my laptop so I can't check but I hope this helps.
If this hasn't outputted the right value, please reply to this so I can try and find the error and edit my answer.
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 4 years ago.
Improve this question
So, before I get into my question. I tried search this but I am probably not wording it correctly to get any valid results. So the purpose is to use in the a AES 128-bit encryption program.
I need to multiply an unsigned char (which would be the hexadecimal value) by 2 or 3 and this would be an XOR operation. So basically, is there a way to do it without typing it out like this.
(SBOX[0] ^ SBOX[0]) ^ SBOX[0]
If I have to do it this way, each line is going to be fairly long but can be done I believe. It would be nice if there is an operator to just say 3 ^ SBOX[0].
If you're doing AES, then you're doing your arithmetic in a Galois Field (specifically GF(28)). Thus rules that you're used to for standard integers no longer hold.
In particular, whilst addition is XOR (in GF(2n)), multiplication isn't repeated addition. Your example shows why - multiplication by two would be x ^ x == 0 always.
The actual steps (in code) depend on the reducing polynomial of your Galois field (and in any case, deriving them is way beyond my ability nowadays). However, they're summarised in multiple places on the web. And in many case, these explanations specifically target the S-box MixColumns operation, e.g. Wikipedia.
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 5 years ago.
Improve this question
I'm have to write a C program, what it does is takes a integer as input. And gives output to the input(th) number of PI after decimal. Like if input is 100, output will be 9(100th digit of pi, after decimal is 9).
What will be the best algorithm for that?
p.s I don't want to save the value of pi into the string, and do it.
Your question is more a math question than a C programming one (so perhaps off-topic). Read first the wikipage on Pi and Approximations of π
If you need to compute only a few hundred (or even hundred thousands) digits, you just need to use some algorithm and code it using some bignum library (e.g. GMPlib, which has mpfr_const_pî ass commented by chtz).
Things become interesting for many billions of digits. I'm not expert on Pi, but look into Fabrice Bellard work on it (read the technical notes mentioning Chudnovsky's algorithm).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is there any objective reason, besides readability or tradition, to declare float constants with a zero fractional part as 1.0f as opposed to simply 1.f?
This may simply be opinion based, but pretty much all the code I've seen online has it that way. Is it aiming to make adding a fractional part later easier (doubtful, since it involves one more erase) or simply for readability sake?
Should there be a difference between C and C++ regarding this, include both flavors.
As you point out, it is not necessary in C, nor C++ to have digits after the decimal point, not indeed before if some are present after. It does not make a difference and is solely a matter of style to write 1.0f or 1.f.
Other languages differ on this syntactic choice to allow for the decimal point to be parsed as the member dereference operator.
In Ruby for example, 1.f would be parsed as 1 . f attempting to read property f of the number 1.
No. It's purely readability and tradition. It does not differ between C and 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 9 years ago.
Improve this question
I am supposed to build a program for storing and handling huge integers. I know that there are many answers out there but I need ideas that I can implement easily, bearing in mind that I can use any of the basic concepts of C/C++.
How should I go about it?
This is the first time I am asking a question here so please correct me if I am wrong about anything.
Edit: Actually what I wanted to know was how should I go about storing a huge integer... Obviously an array is what comes to mind at first glance but are there any other methods out there at the basic level?
EDIT2: I came across a very nice solution to this problem a while ago, but was just a bit lazy to put it on here. We can use the concept of number systems to deal with huge numbers. We can declare an array that holds the co-efficient of powers of 256, thus obtaining a base 256 system. We can then use fundamental concepts like those of the various number systems to obtain our required results.
Matt McCutchen has a Big Integer Library
If you want to do this yourself his code would be a great starting point. As you can overload arithmetic operators in C++ it is not too difficult to make a new BigInteger class and make this handle any number of bits per integer.
There is also a stack overflow answer to this question: here
I consider this as a question about theory, as such I suggest to browse the internet using the right keywords for documents/articles or to take a sneak peek at libraries that are implementing this feature and are well tested, this projects also tend to offer a mailing list or a forum where developers can communicate, it can be a good place to start writing about this stuff.