NEWTON

Asked

4 months ago

142

views

1

Hello! This is day 3 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Conditionals”. Perhaps you can help me?

`// The function `abs_eq` should return 1 if its inputs // are equal or differ only in sign and 0 otherwise. // Its current implementation is incorrect. // // 1. Click on "Run" to see where it fails. // 2. Fix the body of `abs_eq`, so that the // program terminates successfully. // Returns 1 if |x| = |y| (that is, x = y or x = -y), // 0 otherwise. func abs_eq(x: felt, y: felt) -> (bit: felt) { if (x == -y) { return (bit=1); } else { return (bit=0); } } func main() { let (bit) = abs_eq(2, -2); assert bit = 1; let (bit) = abs_eq(2, 3); assert bit = 0; let (bit) = abs_eq(2, 2); assert bit = 1; return (); }`

Answers to this question are a part of the ✨

17 days of Cairo Lang with Playground & Newton.✨

*Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules*

- #17daysOfCairocairo-beginnerscairoplayground

N

Newton

asked

4 months ago

3

Accepted answer

One of the ways to implement the required function would be to add a check on the situation where `x == y`

. You could also remove the else which is redundant and just return `bit = 0`

if your function reaches the end of execution. You would get something like this:

```
func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == y) {
return(bit=1);
}
if (x == -y) {
return (bit=1);
}
return(bit=0);
}
```

answered

4 months ago

0

...of IFs. Yes Cairo doesn't support logic operators. (like ||)

So you need to add:

```
if (x == y) {
return (bit=1);
}
```

v

victorforissier.eth

answered

3 months ago

0

Unfortunately seems like Cairo doesn't support logical operators (|| , &&, !) so something like this is not possible:

```
if (x == -y || x == y) {
return (bit=1);
}
```

So the solution is to use multiple `if-s`

(or one if and use `abs_value`

as other users pointed out)

```
func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == -y) {
return (bit=1);
}
if (x == y) {
return (bit=1);
} else { // else can be omitted, but if it's not, it must be in the same line as {
return (bit=0);
}
}
```

I

Ivan

answered

4 months ago

1

I wondered that such a basic function as `abs`

should exist on cairo. Therefore, I went through the cairo-lang repository and found the function I wanted.

However, I had a problem with the implementation of the `abs`

function. But thanks to the help from the cairo community I was able to solve it. Here is the error

My answer for the day 3

```
%builtins range_check
from starkware.cairo.common.math import abs_value
func abs_eq{range_check_ptr}(x: felt, y: felt) -> (bit: felt) {
if (abs_value(x) == abs_value(y)) {
return (bit=1);
} else {
return (bit=0);
}
}
func main{range_check_ptr}() {
let (bit) = abs_eq(2, -2);
assert bit = 1;
let (bit) = abs_eq(2, 3);
assert bit = 0;
let (bit) = abs_eq(2, 2);
assert bit = 1;
return ();
}
```

c

chocolaite

answered

4 months ago

0

```
// The function `abs_eq` should return 1 if its inputs
// are equal or differ only in sign and 0 otherwise.
// Its current implementation is incorrect.
// Returns 1 if |x| = |y| (that is, x = y or x = -y),
// 0 otherwise.
func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == -y) {
return (bit=1);
} else {
return (bit=0);
}
}
func main() {
let (bit) = abs_eq(2, -2);
assert bit = 1;
let (bit) = abs_eq(2, 3);
assert bit = 0;
let (bit) = abs_eq(2, 2);
assert bit = 1;
return ();
}
```

```
func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == y) { //2===2 or -2 ==. -2
return (bit=1);
}
if (x == -y) { // 2 == -(-2) or -2 == -2
return (bit=1);
}
return (bit=0);
}
func main() {
let (bit) = abs_eq(2, -2);
assert bit = 1;
let (bit) = abs_eq(2, 2);
assert bit = 1;
let (bit) = abs_eq(-2, 2);
assert bit = 1;
let (bit) = abs_eq(-2, -2);
assert bit = 1;
let (bit) = abs_eq(2, 3);
assert bit = 0;
return ();
}
```

**abs_eq** function accepts two arguments x and y, if x and y equal in magnitude or differ only in sign it will return 1 otherwise 0.

**First if statement** checks the both magnitude and signs should be equal to return true that is (2,2), (-2,-2)

**Second if statement** checks the both magnitude should be same but return true only if signs are different that is (-2,2) , (2,-2)

main function uses **assert** to verifies the computation of all the test cases possible for the conditions.

IR

Ishita Rastogi

answered

4 months ago

0

// The function `abs_eq`

should return 1 if its inputs
// are equal or differ only in sign and 0 otherwise.
// Its current implementation is incorrect.
//
// 1. Click on "Run" to see the where it fails.
// 2. Fix the body of `abs_eq`

, so that the
// program terminates successfully.

// Returns 1 if |x| = |y| (that is, x = y or x = -y), // 0 otherwise. func abs_eq(x: felt, y: felt) -> (bit: felt) { if( x == y){ return (bit=1); } if (x == -y) { return (bit=1); } else { return (bit=0); } }

func main() { let (bit) = abs_eq(2, -2); assert bit = 1;

```
let (bit) = abs_eq(2, 3);
assert bit = 0;
let (bit) = abs_eq(2, 2);
assert bit = 1;
return ();
```

}

answered

4 months ago

0

```
func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == -y) {
return (bit=1);
}
if (x == y) {
return (bit=1);
}
return (bit=0);
}
func main() {
let (bit) = abs_eq(2, -2);
assert bit = 1;
let (bit) = abs_eq(2, 3);
assert bit = 0;
let (bit) = abs_eq(2, 2);
assert bit = 1;
return ();
}
```

A

Asten

answered

4 months ago

0

func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == y) {
return(bit = 1);
}
if (x == -y) {
return(bit = 1);

}
return (bit = 0);
}

func main() { let (bit) = abs_eq(2, -2); assert bit = 1;

```
let (bit) = abs_eq(2, 3);
assert bit = 0;
let (bit) = abs_eq(2, 2);
assert bit = 1;
return ();
```

}

0x88E3...2c3B6F

answered

4 months ago

1

```
func abs_eq(x: felt, y: felt) -> (bit: felt) {
//return 1 if x = -y
if (x == -y) {
return (bit=1);
}
//return 1 if x = y
if (x == y) {
return (bit=1);
}
//otherwise
return (bit=0);
}
```

answered

4 months ago

1

```
func abs_eq(x: felt, y: felt) -> (bit: felt) {
if (x == y) {
return (bit=1);
}
if (x == -y) {
return (bit=1);
}
return (bit=0);
}
```

a

ac

answered

4 months ago

1

Cairo OR operator

0

How to use get_fp_and_pc in Cairo Lang?

1

What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?

3

How to write a function in Cairo Lang (StarkNet)?

0

Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?

1

How to submit a StarkNet contract?

0

How to make recursive function in Cairo Lang?

0

Why does Starkware pedersen Hash Function split numbers in high and low bits

0

What's the best advanced StarkNet guide?

0

Cairo: Else if statement

0

Can anyone share some articles explaining SNARK/STARK concepts and rollups (Optimism)?

0

How do I use Create React App in StarknetJS?

0

Where can I learn more about starknet?

0

Cairo/Nile: Testing the proxy upgrade feature, but my tests fail with the error message TypeError: int() can't convert non-string with explicit base.