0

I have written a conditional constant where I want to tell the the machine what it already knows to define a rule. however, all outputs are set to 0.0000 floating. That's not the issue, the issue is, I was expecting a 1 or -1 in certain conditions.

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>

float zero = 0.0;
float pos = 1.0;
float neg = -1.0;
float RULE;
float x, y, z;

//#define RULE (neg < zero < pos);

float main() {

    #define RULE (neg < zero < pos);

    float a, b, c, d, e;
    
    a = RULE(pos > zero) ? pos : zero;
    b = RULE(zero > pos) ? zero : pos;
    c = RULE(neg > pos) ? neg : pos;
    d = RULE(neg > zero) ? neg : zero;
    e = RULE(pos > neg) ? pos : neg;
    
    printf("(pos > zero) pos ?: zero %f \n", a);
    printf("(zero > pos) zero ?: pos %f \n", b);
    printf("(neg > pos) neg ?: pos %f \n", c);
    printf("(neg > zero) neg ?: zero %f \n", d);
    printf("(pos > neg) pos ?: neg %f \n", e);
    
    return 0;

}
bolov
  • 72,283
  • 15
  • 145
  • 224
  • Forgot to mention I am new to C. Terminal Output: (pos > zero) pos ?: zero 0.000000 (zero > pos) zero ?: pos 0.000000 (neg > pos) neg ?: pos 0.000000 (neg > zero) neg ?: zero 0.000000 (pos > neg) pos ?: neg 0.000000 – Russell Clarke Sep 27 '20 at 13:02
  • [Can we have an official statement regarding greetings, salutations etc. please?](https://meta.stackoverflow.com/questions/351588/can-we-have-an-official-statement-regarding-greetings-salutations-etc-please) – bolov Sep 27 '20 at 13:31

1 Answers1

2

There are a lot of things completely wrong with this. I highly suggest picking up a good C book before proceeding. Check this thread for good C books.

Now, to explain what's wrong. I honestly have no idea what you are trying to do so I'll just explain what you're doing wrong, and the reasons behind the behavior you're seeing.

Firstly, float main() is not a valid signature for the main function. According the C standard, the main function must return integers.

5.1.2.2.1

Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

 int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

 int main(int argc, char *argv[]) { /* ... */ }

or equivalent;10) or in some other implementation-defined manner.

So definitely change that to int main(void) first. (int main() also works but they are not actually the same)

Now, this macro-

#define RULE (neg < zero < pos);

So, this is how this #define macro works- wherever it sees RULE in your code, (i.e RULE(pos > zero) ? pos : zero;) it replaces it with (neg < zero < pos);

So, when you write-

a = RULE(pos > zero) ? pos : zero;

It becomes,

a = (neg < zero < pos);(pos > zero) ? pos : zero;

Which is the same as-

a = (neg < zero < pos);
(pos > zero) ? pos : zero;

Yeah.....that makes no sense....at all. Every single variable is just storing the result of (neg < zero < pos) and the ternary ((pos > zero) ? pos : zero;) is being completely ignored, because the result is not stored anywhere.

Please study carefully how #define works, it's not difficult - trust me :). Here's is a pretty good guide at cppref

But wait! there's more- (neg < zero < pos) <- this doesn't do what you probably think it does. This just gets grouped to-

(neg < zero) < pos

So this first evaluates-

neg < zero

Which is obviously 1 (aka true), because indeed, -1.0 is less than 0.0 Then it compares this result to pos-

1 < pos

pos is 1.0; Comparing floats to ints is a bad idea, both 1 < 1.0 and 1 > 1.0 may evaluate to 0 (i.e false) which is what your machine is doing. Read this

So those are the reasons why it's always 0. Your ternaries are completely ignored every single variable gets the result of the same calculation, that is (neg < zero < pos)

Once again, I suggest following a good C book. Whoever taught you to write float main certainly is not a good teacher, not when it comes to C.

Chase
  • 5,315
  • 2
  • 15
  • 41