Skip to content Skip to sidebar Skip to footer

If X>=1, Then 1, Otherwise 0, As Pure Math Equation?

Due to the constraints of some dev tools, I'm looking for a mathematical expression of: If x>=1: y = 1 else: y = 0 if it adds simplicity, X must be an integer greater o

Solution 1:

I'd say there is no way to get a discontinue function defined everywhere like y(x) using a finite number of continuous functions with composition and continuous operators only.

You can get something like abs(x) with (x ** 2) ** 0.5 but I don't see how you can use this (that has a discontinuous first derivative but that is still continuous) to get a perfect step function defined everywhere with assigned value at step point.

Something like 0.5 + 0.5 * abs(x-1)/(x-1) is what you're looking for almost everywhere, but you're going to have problems for the singularity x=1 where 0/0 is going to be evaluated.

EDIT

If input x is guaranteed to be an integer then the solution is easy:

defy(x):
    return0.5 + 0.5 * abs(x - 0.5) / (x - 0.5)

or, defining abs using power...

defy(x):
    return0.5 + 0.5 * ((x - 0.5) ** 2) ** 0.5 / (x - 0.5)

The function is undefined for x=0.5 but if it's evaluated only on integers this is not a problem.

Another option is

defy(x):
    return0.5 + abs(x - 0.25) + (x - 0.25) - abs(abs(x-0.25) + (x-0.25) - 0.5)

that is a continuous function that is 0 before 0.25, goes linearly from 0 to 1 as x goes from 0.25 to 0.75 and stays to 1 after it.

Solution 2:

In this case I would suggest the following: As x is integer and x>=0 the following logic wil be true:

if x > 0 then 0**x == 0 else 0**x == 1. This is equal to if x >=1 then 0**x == 0 else 0**x == 1.

So y = 1 - 0**x will give the desired result.

Solution 3:

Lets see here, you basically have an inequality x-1>=0.

y = (x-1)/abs(x-1) will give you the value you are looking for for all cases except for when x=0. In this case an exception will be thrown, perhaps you can use that to set the value of y. so something like:

try{
y=(x-1)/abs(x-1);
} catch(ArithmaticException e){
y =1}

Solution 4:

Since the integer is known to be non-negative, one can simply OR together all of the bits of the integer one-by-one. The result is the desired predicate. A simple loop that performs bit-wise OR-ing until the source operand is exhausted requires a comparison, which is not allowed.

The only workable alternative under the restrictions imposed that I can see right now is to use straight-line code that repeats the bit extraction process for as many steps as the integer has bits. This is what I use in the ISO-C99 code below. Each bit is extracted with DIV and MOD. OR of two one-bit variables s and t is computed as s + t - s * t. A simple exhaustive test for 32-bit integers confirms that this approach if functional, but the efficiency is obviously not great.

#include<stdio.h>#include<stdint.h>#include<stdlib.h>#define OR1(s,t)  (s + t - s * t)// (x >= 1) ? 1 : 0uint32_tge1(uint32_t x)
{
    uint32_t y;

    y = OR1 (
             OR1 (
                 OR1 (OR1 (OR1 ((x / 0x00000001) % 2, (x / 0x00000002) % 2),
                           OR1 ((x / 0x00000004) % 2, (x / 0x00000008) % 2)),
                      OR1 (OR1 ((x / 0x00000010) % 2, (x / 0x00000020) % 2),
                           OR1 ((x / 0x00000040) % 2, (x / 0x00000080) % 2))),
                 OR1 (OR1 (OR1 ((x / 0x00000100) % 2, (x / 0x00000200) % 2),
                           OR1 ((x / 0x00000400) % 2, (x / 0x00000800) % 2)),
                      OR1 (OR1 ((x / 0x00001000) % 2, (x / 0x00002000) % 2),
                           OR1 ((x / 0x00004000) % 2, (x / 0x00008000) % 2)))), 
             OR1 (
                 OR1 (OR1 (OR1 ((x / 0x00010000) % 2, (x / 0x00020000) % 2),
                           OR1 ((x / 0x00040000) % 2, (x / 0x00080000) % 2)),
                      OR1 (OR1 ((x / 0x00100000) % 2, (x / 0x00200000) % 2),
                           OR1 ((x / 0x00400000) % 2, (x / 0x00800000) % 2))),
                 OR1 (OR1 (OR1 ((x / 0x01000000) % 2, (x / 0x02000000) % 2),
                           OR1 ((x / 0x04000000) % 2, (x / 0x08000000) % 2)),
                      OR1 (OR1 ((x / 0x10000000) % 2, (x / 0x20000000) % 2),
                           OR1 ((x / 0x40000000) % 2, (x / 0x80000000) % 2)))));
    return y;
}

intmain(void)
{
    uint32_t x, res, ref;

    x = 0;
    do {
        res = ge1 (x);
        ref = x >= 1;
        if (res != ref) {
            printf ("error: x=%08x  res=%08x  ref=%08x\n", x, res, ref);
            return EXIT_FAILURE;
        }
        x++;
    } while (x);
    printf ("test passed\n");
    return EXIT_SUCCESS;
}

Solution 5:

For integer x, if abs is OK, then I suggest

y = (x + abs(x)) / (abs(x+1) + abs(x-1))

This is not subject to division by zero, and division is exact for every integer x, positive or negative, as long as no overflow occurs, so it can be evaluated with integer arithmetic.

If you don't care of negative x, it's even simpler:

y = (abs(x+1) - abs(x-1)) / 2

If the function needs to operate on a whole domain of bounded integers, then some larger int arithmetic shall be used, or you can use same function in floating point arithmetic with float(x), or could equally fall back to the answer of @6502 which is simpler in this case.

EDIT if you don't have an absolute value, you could try 1-(2.0**(-x))**n, with large enough n so as to exploit floating point underflow and/or limited precision. n=2000 should be enough to cause underflow for every positive integer input, but no use to have n that high, since n=100 should be enough to cause inexact rounding to 1 on most architectures. Even with floating point arithmetic, it's quite simple to evaluate efficiently, you can use a single power 1.0-(2.0**(-100.0*x)). For negative integer input, replace x with x*x.

Post a Comment for "If X>=1, Then 1, Otherwise 0, As Pure Math Equation?"