I am a beginner to c++ programming and I am given a task of implementation of fixed point math arithmetic in c++. here I am trying to implementation a function isnan() which returns true if the number is not-a-number else will return false.
Test file
#include "fixed_point_header.h"
int main()
{
fp::fixed_point<long long int, 63> a=fp::fixed_point<long long int, 63>::positive_infinity(); // will assign positive infinity value to a from an function from header
fp::fixed_point<long long int, 63> b=fp::fixed_point<long long int, 63>::negative_infinity(); // will assign positive infinity value to b from an function from header
float nan=fp::fixed_point<long long int, 63>::isnan(a,b);
printf( "fixed point nan value == %f\n", float (nan));
}
In the header I want to do somewhat like the code shown below if positive and negative infinity values are added, the isnan function should return 1 else 0.
Header file
#include fixed_point_header
static fp::fixed_point<FP, I, F> isnan (fp::fixed_point<FP, I, F> x,fp::fixed_point<FP, I, F> y){
/*if ( x + y ) happens, ie. x and y are infinities
{
should return 1; }
else {
should return 0; }
} */
can anyone please tell how to proceed with it? or how to solve this paradigm
I am trying to implementation a function isnan() which returns true if the number is not-a-number else will return false.
That's simple enough; define a reserved value to represent nan (as you have for the infinities), and compare with that:
bool isnan(fixed_point x) {
return x == fixed_point::nan();
}
I want to do somewhat like the code shown below if positive and negative infinity values are added, the isnan function should return 1 else 0
It would be the responsibility of the addition operator to check the inputs and return nan if appropriate:
fixed_point operator+(fixed_point x, fixed_point y) {
if (x == fixed_point::nan() || y == fixed_point::nan()) {
return nan;
}
if (x == fixed_point::positive_infinity()) {
return y == fixed_point::negative_infinity() ? fixed_point::nan() : x;
}
// and so on
}
then the test in main becomes:
bool nan = fixed_point::isnan(a+b);
Related
I have this code:
int generatore(std::vector<bool> vettore, int *x) {
int y;
if (std::adjacent_find(vettore.begin(), vettore.end(), std::not_equal_to<>()) == vettore.end() {
return *x + 1;
}
else {
do {
y = rand() % *x;
} while (vettore.at(y) == true);
return y;
}
}
What this function does is basically get a vector that stores boolean values that say if the indexes have already been used and its size, when it is called it should return an index from the ones that hadn't been used or *x+1 if all of them had been.
The vector used as parameter will never be empty.
A false value would mean that that index hasn't been used, the problem with this code is that if all of the values are equal to false it returns *x+1 instead of choosing one random index, so i should only check if all the values are equal to true, what should i change?
Your if condition already checks whether all the values in the vector are the same, so you could check if the first element is also the one you want:
if (vettore.front() == false // add this
&& std::adjacent_find(vettore.begin(), vettore.end(), std::not_equal_to<>()) == vettore.end()) {
// ...
}
Doing this check before the adjacent_find lets the evaluation of the algorithm be potentially avoided.
You could also use the all_of algorithm:
if (std::all_of(vettore.begin(), vettore.end(), [](auto b) { return !b; })) {
// ...
}
In JavaScript there is this isSafeInteger method. How check the same thing in C++? The most straightforward way would be:
bool isSafeInteger(double d) noexcept {
auto const i = static_cast<std::int64_t>(d);
return i == d && i <= 9007199254740991 && i >= -9007199254740991;
}
But it doesn't feel right. Is there a better way to do it?
bool isSafeInteger(double d) noexcept {
if (d>=std::numeric_limits<std::int64_t>::max()) return false;
if (d<=std::numeric_limits<std::int64_t>::min()) return false;
if (isnan(d)) return false;
auto as_int=[](double d){return static_cast<std::int64_t>(d);};
return (as_int(d)==d) && (as_int(d+1)!=as_int(d)) && (as_int(d-1)!=as_int(d));
}
this checks it round trips to double, and that adjacent doubles don't round to the same integer, and that the double isn't a NaN (while svoiding triggering any NaN traps).
Finally, guard against out of bounds conversion, which is UB. We use >= and <= to be safe due to +1/-1 usage later.
This also works for float, but not for 128 (or 70ish) sized floats.
I wonder if I can return error code as double in the following way in C:
double getValue()
{
double ret = 0;
if (error1)
{
return -1;
}
if (error2)
{
return -2;
}
return ret = ....;
}
int main(void)
{
double val = getValue();
if (-1 == val)
{
printf("getValue: error1\n")
return -1;
}
else if (-2 == val)
{
printf("getValue: error2\n");
return -2;
}
......
return 0;
}
so when the return value is >= 0 then it is correct value which can be used for calculations. When value is less than zero error occurred.
Will I get the floating-point-comparison problem when I compare return value with -1 or -2 ?
Flag values are a bad idea. Flag values that are floating point are doubly so, even if double precision.
If you are using IEEE double precision floating point values, the values -1 and -2 are exactly representable as doubles, and comparison is well defined. No "magic error" will slip in if you merely copy the double around or only read the value. In fact, on a system with conventional 2s complement 32 bit ints, every int can be represented exactly as a IEEE double precision floating point value.
Now, transformations you think wouldn't matter like x /3. * 3. will ruin the identity, so the code is very fragile: fragile both because flag values are fragile, and because floating point equivalence is often fragile in practice.
In C++, there are a myriad of ways to do this that are less fragile.
enum error_code {a,b,c};
boost::variant<double, error_code> getValue();
is a tagged union that can hold either a double or an error_code. There is a std::expected proposal you can look at which is a tagged union with a "bias" towards the first value being the only valid one (sort of a cross between std::experimental::optional and boost::variant).
Both of these result in the value being returned in a type-safe way, where an error is a different type of value than the non-error return type.
Alternative solutions include returning the error code separately (as the return value, or taking a pointer-to-error-code as a parameter (what I call ICU style)). The double could be set to some innocuous value in that case (say, NaN) rather than left uninitialized.
double getValue( error_code* e );
or
error_code getValue( double* out );
where enum error_code { a = -1, b = -2 } is an enumeration of the error codes.
#LightnessRacesinOrbit beat me to it, but having typed it I post it anyway.
You can do it by taking the value to be set as a pointer argument, and returning a status. That way, no values of *ret are barred.
int getValue(double *ret)
{
*ret = ...;
if (error1)
return -1;
if (error2)
return -2;
return 0;
}
Then the calling code can be such as
double myval;
int err;
if ((err = getValue(&myval)) == 0)
printf ("getValue() returned %f\n", myval);
else
printf ("getValue() returned error %d\n", err);
Yes, you could get floating-point errors.
So consider using exceptions instead, or perhaps return an int error code and populate a double "out parameter" on success:
int getValue(double& ret)
{
if (error1)
return -1;
if (error2)
return -2;
ret = ....;
return 0;
}
Doing that is not necessary and makes error handling difficult, you should create an enum where you can add or remove error codes as needed, and also you don't really need to remeber what -1 is or what does -2 mean, just give each error a descriptive name, and do this
enum ErrorCodes {NoError, Error1, Error2, ... , ErrorN};
enum ErrorCodes getValue(double *value)
{
if (error1)
return Error1;
if (error2)
return Error2;
.
.
.
if (errorN)
return ErrorN;
*value = resultOfCalculation;
return NoError;
}
then
enum ErrorCode code;
double value;
code = getValue(&value);
switch (code)
{
case NoError:
/* use value here */
break;
case Error1:
/* handle error 1 */
break;
/* and so on */
}
I think this is a lot better and elegant, because you can apply it anytime you want to have robust error checking, no matter what type the target value is, this would work exactly the same for a struct or a double or an int array.
I'm facing an issue regarding the translation in C++ of a bunch of source code written in Visual Basic. In the code there is a call to the method Sign (VB) and various conversions of float to integer... Could you confirm that c++ code for 1, 2, 3 are the same as the VB one? In addition about the implicit conversion I've no idea how the conversion is performed (See 4). Any idea?
1) Method Sign (Visual Basic)
//C++
int sign(float value)
{
if (value < 0) return -1;
else if (value == 0) return 0;
else return 1;
}
2) Method Int (Visual Basic)
//C++
int Int(float value)
{
return ((value >= 0) ? value : floor(value));
}
3) Method CInt (Visual Basic)
//C++
int CInt(const float val)
{
float x = fabs(val - (int)val);
if (fabs(x - 0.5) < 0.0001)
return (int)val;
else
return (int)(val+(val>=0.0?0.5:-0.5));
}
4) And there is also an implicit conversion of double to int. How to make this conversion in c++?
//Visual basic
Dim dt As Integer = -99.2
Thanks you in advance,
1-
It is not the same, floating point values should not be compared to a constant variable (0, in this example). So, this is a better code for it:
const float epsilon = 0.00001f;
if(value < -epsilon) return -1;
if(value > epsilon) return 1;
return 0;
2- It depends on what you want for, for example -5.7. If you want -5, just cast away using (int). for example, if you have a float variable named f, use (int)f. If you want -6, use this function:
int Int(float value)
{
return ((value >= 0) ? (int)value : (int)(value-1));
}
3- It should work but last return statement could be made clearer:
return (int)val + (val>=0.0?1:-1)
4- Doubles are very very similar to floats in C/C++. Do as if you're messing with a float, not double.
I want the source code for java's Float.floatToIntBits() implementation code in C\C++.
This seems to be the best solution:
#include <cstring>
unsigned float_to_bits(float x)
{
unsigned y;
memcpy(&y, &x, 4);
return y;
}
Of course this depends on float and unsigned consuming 4 bytes.
Google Codesearch says
public static int floatToIntBits(float value) {
int result = floatToRawIntBits(value);
// Check for NaN based on values of bit fields, maximum
// exponent and nonzero significand.
if ( ((result & FloatConsts.EXP_BIT_MASK) ==
FloatConsts.EXP_BIT_MASK) &&
(result & FloatConsts.SIGNIF_BIT_MASK) != 0)
result = 0x7fc00000;
return result;
}
Follow the link for the constants.