I just wrote this code for Parrot. I know what it does. I know why it does what it does. I just can't support a language that both allows and requires this:
union { const FLOATVAL *__c_ptr; FLOATVAL *__ptr; } __ptr_u;
#define const_cast_float(b) (__ptr_u.__c_ptr = &(b), __ptr_u.__ptr)
#if INTVAL_SIZE == DOUBLE_SIZE
# define FLOAT_IS_ZERO(f) (*(INTVAL *)(const_cast_float(f)) == 0)
#else
# define FLOAT_IS_ZERO(f) (*(HUGEINTVAL *)(const_cast_float(f)) == 0)
#endif
Tradeoffs (Score:1)
Re: (Score:1)
Re: (Score:2)
Don't "support" it then... (Score:1)
You say you can't support such a language, but your actions say something different. You're developing for a project, Parrot, that is implemented in C supports the language.
If you don't want to support the language C, write Parrot in something else. What's that? C has many advantages in terms of portability and performance over alternatives for a project like Parrot? Also, C has this huge installed base, which guarantees high-quality i
Re: (Score:1)
Stop, you're killing me!
I wouldn't be trying to replace it, that's for sure.
Re: (Score:1)
So, what is it that you find funny above? That C compilers are of high quality or that there's a large, experienced programming community surrounding C or both?
I guess you could criticize C compilers for quality, but everything's relative. What language implementations have higher quality compilers available?
The experience and size of the C programmin
Re: (Score:1)
Ever try to write a cross-platform C99 application? The standard's most of a decade old now. It'd be lovely if compilers could support it. (Microsoft only has a few thousand programmers. I'm sure they'll get around to it eventually.)
I'd also love to meet a few members of this large, experienced programming community. I could put two or three of them to work!
Re: (Score:1)
I have to admit, I haven't done a lot of checking for strict C99 compatibility. From what I'm given to understand, everybody except Microsoft does an excellent job of supporting the C99 standard.
Microsoft not supporting cross platform development. Shocking!!
wtf (Score:1)
const FLOATVAL cf; float * f = (FLOATVAL *)?Re: (Score:1)
Compilers warn (rightfully so) that comparing floating point values is notoriously inaccurate. One decent way to compare them anyway is to cast them to pointers and then compare them as integers. Of course, to do that, sometimes you have to cast away constness, hence the ugly, awful, horrible union hack.
Re: (Score:1)
Why can’t you just use
#if INTVAL_SIZE == DOUBLE_SIZE
?# define FLOAT_IS_ZERO(f) (*(INTVAL *)&(f) == 0)
#else
# define FLOAT_IS_ZERO(f) (*(HUGEINTVAL *)&(f) == 0)
#endif
Re: (Score:1)
FLOATVAL*.gcc -Walldoesn't carp about it on my system:const float cf;
float * f = (float *)&cf;
Re: (Score:1)
gcc -Walldoesn't report very many errors. We have-Wcast-qualenabled, which does warn on that construct.Re: (Score:1)
-Wcast-qual. I tend to just assume that since a cast says that you deliberately chose to break the rules, it's always a sign that the programmer decided to do something risky. I don't find it particularly helpful for the compiler to complain about the rule he broke, since he already broke it on purpose. YMMV, of course.Re: (Score:1)
It's awfully easy to cast away constness accidentally, as you have to put
conston all of the casts to retain it. It's a useful warning.Re: (Score:1)
So the warning is valid but should be ignored here.
I'd prefer the simpler-but-generates-a-warning over the ugly-mess-to-work-around-warning code. Unless you've got
-Werror, which is just annoying, and there really should be a way to fine-tune warnings (because you really do know what you're doing here).Come to think of it, what about
#if INTVAL_SIZE == DOUBLE_SIZE# define FLOAT_IS_ZERO(f) (*(const INTVAL *)&(f) == 0)
#else
# define FLOAT_IS_ZERO(f) (*(const HUGEINTVAL *)&(f) ==