Moving code from Visual Basic to C++ issue - c++

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.

Related

Determine if a double value isSafeInteger in C++?

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.

implementation of isnan() function

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);

Rational Vector Assertion

Below is the code in C++,
Num *= other.Den;
Den *= other.Num;
if (Den.isNegative()) {
Num = -Num;
Den = -Den;
}
assert(Den.isStrictlyPositive());
where Num and Den are of type LLVM::APInt.
For some reason I am getting the assertion failed. I have checked if the Denominator is negative explicitly and turned it positive. Can someone please let me in what scenario in this code, the assertion can fail? When I run my code against test case it fails. The test case is very large, and I have not been successful in cornering a particular case. The above code is a part of my algorithm which is doing some other job.
Here is the implementation of isStrictlyPositive. It is using the LLVM library file APInt.h.
bool isStrictlyPositive() const {
return isNonNegative() && !!*this;
}
bool isNonNegative() const {
return !isNegative();
}
I'm basing this off the following assumptions:
Strictly positive means > 0
isNegative is < 0
Given the snippet you quoted, the function isStrictlyPositive boils down to:
return isNonNegative() && !!*this;
Which is equivalent to:
return !(*this < 0) && !!*this;
!!*this is equivalent to !(!*this) which is equivalent to !(*this==0) which is equivalent to *this!=0, so the expression is:
return !(*this < 0) && *this!=0;
Which can be simplified to:
return *this>=0 && *this!=0;
Which is really just:
return *this > 0;
So, your issue is that Den is 0 and is therefore not negative and not strictly positive.
0 is neither negative nor strictly positive.

Removing Virtual Inheritance

I am working on an embedded project I am trying to remove a virtual number class that has + / - * implemented. removing this class saves a lot of code space so I have replaced + with the following function,
if (BASE(h)->type() == FLOAT && BASE(v)->type() == FLOAT)
{
res = FLOAT(h)->floatValue() + FLOAT(v)->floatValue();
}
else if (BASE(h)->type() == INTEGER && BASE(v)->type() == INTEGER)
{
res = INTEGER(h)->intValue() + INTEGER(v)->intValue();
}
else if (BASE(h)->type() == INTEGER && BASE(v)->type() == FLOAT)
{
res = INTEGER(h)->floatValue() + FLOAT(v)->floatValue();
}
else
{
res = FLOAT(h)->floatValue() + INTEGER(v)->floatValue();
}
Is there a less uglier way to achieve this? cause I have to use the same scheme for other ops and comparison?
#define GETFLOAT(arg) (BASE(arg)->type() == INTEGER ? INTEGER(arg)->floatValue() : FLOAT(arg)->floatValue())
switch(BASE(h)->type()) {
case INTEGER:
if (BASE(v)->type() == INTEGER) {
res = INTEGER(h)->intValue() + INTEGER(v)->intValue();
break;
}
case FLOAT:
res = GETFLOAT(h) + GETFLOAT(v);
}
This actually branches on the type of h twice, but only in the case that (you say in a comment elsewhere) is expensive anyway, the floating-point op. You could avoid that with a goto, but I'm not going to have that argument again. Something like:
switch(BASE(h)->type()) {
case INTEGER:
if (BASE(v)->type() == INTEGER) {
res = INTEGER(h)->intValue() + INTEGER(v)->intValue();
goto finished; // or better: return res;
}
hvalue = INTEGER(h)->floatValue()
break;
case FLOAT:
hvalue = FLOAT(h)->floatValue();
}
res = hvalue + GETFLOAT(v);
finished:
As with Howard's answer, if BASE() or type() is expensive then you could calculate the answer for each argument once, even though it's used twice.
What about doing it in two steps?
isInt1 = BASE(h)->type()==INTEGER;
isInt2 = BASE(v)->type()==INTEGER;
if (isInt1 && isInt2)
op1 = INTEGER(h)->intValue();
op2 = INTEGER(h)->intValue();
res = op1 + op2;
else {
op1 = isInt1 ? (FLOAT(h)->floatValue()) : (INTEGER(h)->floatValue());
op2 = isInt2 ? (FLOAT(v)->floatValue()) : (INTEGER(v)->floatValue());
res = op1 + op2;
}
I recommend reconsidering your architecture. How much have you saved with this approach, and how much has it cost in performance? Your new approach appears pushes everything to a float (you didn't show the declaration of res, which I presume is float res;.
Examine what this fix does to something like (a+b)*c, where each of a, b, and c are (were) integers. With this fix at hand, you now have a float times an int, which is a lot more expensive computationally than an int times an int.
I suggest using templates and letting C++ type system handle as much of the conversion as possible. This lets you use disparate storage types that don't have to have common virtual base class.
You can also cut down on program size by implementing only one of int+float versus float+int (and similarly with int*float versus float*int). Temporarily implement both int+float and float+int, but intentionally make one raise a compile-time error. Flip the order of the operands where you get failures.

Java's Float.floatToIntBits implementation code in C\C++

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.