How to check if a variable has a number in desmos

I’m trying to make it so that you can do binary to decimal, but I don’t want them to be able to put a number like two that your return is an error.

Your link is a blank graph FYI.

2 Likes

Binary to Decimal example

I’m not sure how to do this other than by turning the original number into a list of digits, so that you can examine each one separately. You can’t do that directly, but you can use math functions to determine the number of digits in a given number, and you can calculate the digit at a given place value. So, I put that together, and used list comprehensions to turn the given binary number into a list of bits. I made it so that it returns a -1 for the digit if it is anything other than 1 or 0.

So then, I iterate across the list to see if any of the digits are -1. I put that into a check function that returns 1 if all digits are 1 or 0, and 0 otherwise.

Finally, it’s just matter of multiplying each bit by 2^(place value) and adding them up.

If you are new to list comprehensions, you can find out more about them here.

Feel free to ask if you have questions about any of this!

What do you mean by this? Pretty confusing… could you please reword it?

@Daniel_Wekselgreene… your bin->num is a little broken as any binary input other than 11100000 just returns -1, unless this was intended.

I’m not sure what you mean. It seems to be working fine for me? It only returns -1 if there is a digit other than 1 or 0 in the number.

Oh sorry. I wasn’t paying attention to what num actually equaled… you are correct if num is like 300000 it will be -1. My mistake.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.