|
|
Home » U++ Library support » U++ Core » Value with type float
|
Re: Value with type float [message #58367 is a reply to message #58295] |
Tue, 10 May 2022 13:21   |
Tom1
Messages: 1080 Registered: March 2007
|
Senior Contributor |
|
|
Hi Mirek,
Sorry to return to this subject, but could you consider adding Null support for float?
I had something like this in mind... (in Core/Defs.h):
const int INT_NULL = INT_MIN;
const int64 INT64_NULL = INT64_MIN;
constexpr double DOUBLE_NULL = -std::numeric_limits<double>::infinity();
constexpr float FLOAT_NULL = -std::numeric_limits<float>::infinity();
class Nuller {
public:
operator int() const { return INT_NULL; }
operator int64() const { return INT64_NULL; }
operator double() const { return DOUBLE_NULL; }
operator float() const { return FLOAT_NULL; }
operator bool() const { return false; }
Nuller() {}
};
extern const Nuller Null;
template <class T> void SetNull(T& x) { x = Null; }
template <class T> bool IsNull(const T& x) { return x.IsNullInstance(); }
template<> inline bool IsNull(const int& i) { return i == INT_NULL; }
template<> inline bool IsNull(const int64& i) { return i == INT64_NULL; }
template<> inline bool IsNull(const double& r) { return !(std::abs(r) < std::numeric_limits<double>::infinity()); }
template<> inline bool IsNull(const float& r) { return !(std::abs(r) < std::numeric_limits<float>::infinity()); }
template<> inline bool IsNull(const bool& r ) { return false; }
Although, I'm not entirely sure, if this is completely correct way to do it.
Best regards,
Tom
PS. EDIT: I think the above should work mostly. Only the "Cout() << FLOAT_NULL;" prints -inf instead of empty field, which is the default for "Cout() << DOUBLE_NULL;":
CONSOLE_APP_MAIN{
double d=Null;
float f=Null;
double a=f;
float b=d;
Cout() << "d = " << d << "\n";
Cout() << "f = " << f << "\n";
Cout() << "a = " << a << "\n";
Cout() << "b = " << b << "\n";
Cout() << "a == f : " << (bool)(a==f) << "\n";
Cout() << "b == d : " << (bool)(b==d) << "\n";
Cout() << "IsNull(d) = " << IsNull(d) << "\n";
Cout() << "IsNull(f) = " << IsNull(f) << "\n";
Cout() << "IsNull(a) = " << IsNull(a) << "\n";
Cout() << "IsNull(b) = " << IsNull(b) << "\n";
}
[Updated on: Tue, 10 May 2022 13:31] Report message to a moderator
|
|
|
Re: Value with type float [message #58368 is a reply to message #58367] |
Tue, 10 May 2022 15:44   |
 |
mirek
Messages: 13623 Registered: November 2005
|
Ultimate Member |
|
|
Tom1 wrote on Tue, 10 May 2022 13:21Hi Mirek,
Sorry to return to this subject, but could you consider adding Null support for float?
I had something like this in mind... (in Core/Defs.h):
const int INT_NULL = INT_MIN;
const int64 INT64_NULL = INT64_MIN;
constexpr double DOUBLE_NULL = -std::numeric_limits<double>::infinity();
constexpr float FLOAT_NULL = -std::numeric_limits<float>::infinity();
class Nuller {
public:
operator int() const { return INT_NULL; }
operator int64() const { return INT64_NULL; }
operator double() const { return DOUBLE_NULL; }
operator float() const { return FLOAT_NULL; }
operator bool() const { return false; }
Nuller() {}
};
extern const Nuller Null;
template <class T> void SetNull(T& x) { x = Null; }
template <class T> bool IsNull(const T& x) { return x.IsNullInstance(); }
template<> inline bool IsNull(const int& i) { return i == INT_NULL; }
template<> inline bool IsNull(const int64& i) { return i == INT64_NULL; }
template<> inline bool IsNull(const double& r) { return !(std::abs(r) < std::numeric_limits<double>::infinity()); }
template<> inline bool IsNull(const float& r) { return !(std::abs(r) < std::numeric_limits<float>::infinity()); }
template<> inline bool IsNull(const bool& r ) { return false; }
Although, I'm not entirely sure, if this is completely correct way to do it.
Best regards,
Tom
PS. EDIT: I think the above should work mostly. Only the "Cout() << FLOAT_NULL;" prints -inf instead of empty field, which is the default for "Cout() << DOUBLE_NULL;":
CONSOLE_APP_MAIN{
double d=Null;
float f=Null;
double a=f;
float b=d;
Cout() << "d = " << d << "\n";
Cout() << "f = " << f << "\n";
Cout() << "a = " << a << "\n";
Cout() << "b = " << b << "\n";
Cout() << "a == f : " << (bool)(a==f) << "\n";
Cout() << "b == d : " << (bool)(b==d) << "\n";
Cout() << "IsNull(d) = " << IsNull(d) << "\n";
Cout() << "IsNull(f) = " << IsNull(f) << "\n";
Cout() << "IsNull(a) = " << IsNull(a) << "\n";
Cout() << "IsNull(b) = " << IsNull(b) << "\n";
}
IDK. Does it solve any real problem?
Up until this year, I have considered double-float relation to be similar to int-int16. You use float to reduce memory consumption or for special things (GPU), but you really do not need to support it in Value or widgets. Served me well for many many years. What has changed? 
Mirek
|
|
|
Re: Value with type float [message #58369 is a reply to message #58368] |
Tue, 10 May 2022 20:08   |
Tom1
Messages: 1080 Registered: March 2007
|
Senior Contributor |
|
|
Hi,
What has changed is that I have really started to enjoy the new EditFloatSpin! 
As a result, initializing and reading my float variables to/from EditFloatSpin (as used in e.g. filter parameters) is clean and easy. However, I found that initializing my EditFloatSpin to empty field (e.g. when such filtering is not currently used) requires my float variable to be at Null:
EditFloatSpin hpfedit;
...
float hpf=(float)(double)Null;
...
hpfedit <<= hpf;
Alternatively, some value of hpf (e.g.< 0) could be interpreted as empty filtering and I could do hpfedit.Clear(); if such is the case.
However, I prefer nice, short and clean code, so I would like to write just:
I.e. without any type cast. So, in effect, this is just to make the code cleaner.
Best regards,
Tom
[Updated on: Tue, 10 May 2022 20:28] Report message to a moderator
|
|
|
Re: Value with type float [message #58370 is a reply to message #58369] |
Tue, 10 May 2022 21:26   |
 |
mirek
Messages: 13623 Registered: November 2005
|
Ultimate Member |
|
|
Tom1 wrote on Tue, 10 May 2022 20:08Hi,
What has changed is that I have really started to enjoy the new EditFloatSpin! 
As a result, initializing and reading my float variables to/from EditFloatSpin (as used in e.g. filter parameters) is clean and easy. However, I found that initializing my EditFloatSpin to empty field (e.g. when such filtering is not currently used) requires my float variable to be at Null:
EditFloatSpin hpfedit;
...
float hpf=(float)(double)Null;
...
hpfedit <<= hpf;
Alternatively, some value of hpf (e.g.< 0) could be interpreted as empty filtering and I could do hpfedit.Clear(); if such is the case.
However, I prefer nice, short and clean code, so I would like to write just:
I.e. without any type cast. So, in effect, this is just to make the code cleaner.
Best regards,
Tom
Why dont you just use
double hpf;
?
|
|
|
Re: Value with type float [message #58373 is a reply to message #58370] |
Wed, 11 May 2022 08:20  |
Tom1
Messages: 1080 Registered: March 2007
|
Senior Contributor |
|
|
Quote:
Why dont you just use
double hpf;
?
Well, all my signal processing code runs on 32-bit floats, and therefore, the various coefficients/parameters are also floats. It is straight forward to keep it up in the user interface too. If I used doubles in the GUI, I would end up converting forth and back all those parameters. Never wish to go back there, now that I have EditFloat and EditFloatSpin! 
Anyway, if you feel seriously reluctant to add Null support for float, I can live with it: I figured out a way to do it outside of Core almost cleanly:
constexpr float Nullf = -std::numeric_limits<float>::infinity();
inline bool IsNull(const float& r) { return !(std::abs(r) < std::numeric_limits<float>::infinity()); }
inline void SetNull(float& x) { x = Nullf; }
Still, it would be nicer inside Core... After all, it would introduce only three lines of new code in Core/Defs.h.
Best regards,
Tom
|
|
|
Goto Forum:
Current Time: Thu May 26 23:42:43 CEST 2022
Total time taken to generate the page: 0.01637 seconds
|
|
|