NEWTON

Asked

6 months ago

148

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

6 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

6 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

5 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

6 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

6 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

6 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

6 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

6 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

6 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

6 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

6 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

Is there an example of migration scripts somewhere [Cairo/StarkNet]?

0

Will Kakarot be an L3 on top of the Starknet validity rollup?

0

How to modify keccak.cairo and/or packed_keccak.cairo to compute a 512bits keccak hash instead of 256 bits?

0

ApeWorX: How do I fund newly created accounts from the ape console?

0

Are there situations in which we prefer declaring variables as `local` vs `tempvar`?

0

AssertionError with range_check_ptr_ptr

0

Would anyone have a link or know how fast blocks would be generated on StarkNet after Regenesis ?