Checking to see if a number is rational

I’m wondering how to check to see if a number a student enters is rational or irrational. I know in the calculator a little fraction symbol pops up if the number entered is rational but I don’t know if that can be accessed through CL for checking.

Can you be more specific about what a student may be entering? There’s not anything regarding rationality in CL. Closest would be to use patterns in some way, I think.

this works pretty well for me:

p=patterns
num=p.number
frac=p.fraction(p.integer,p.integer)
mixed=p.mixedNumber
int=p.integer
radicalRootSpecified=p.radical(p.expression,p.expression)
radical=p.radical(p.expression) #checks for a radical where the index is not specified(ie square root)
numerator = frac.parse(this.latex).numerator
denominator = frac.parse(this.latex).denominator

#checking if a perfect root is expressed. unlikely but you never know:
ans=numericValue("${this.latex}")
decimalPart=numericValue("\operatorname{mod}\left(${ans},1\right)")

#correctness
checkForm = not(num.matches(this.latex) or frac.matches(this.latex) or int.matches(this.latex) or mixed.matches(this.latex) or (radicalRootSpecified.matches(this.latex) and decimalPart=0) or (radical.matches(this.latex) and decimalPart=0)) 
#check = checkForm and countNumberUsage(this.latex)=1 #this should work but Desmos does not treat pi and e as numbers!
1 Like

I ended up using Gemini to help me code it and here is what it came up with for a 2x2 table where in the first column students need to enter a rational, and in the 2nd an irrational.

p = patterns
check = “:white_check_mark:
cross = “:cross_mark:

— ROW 1, COL 1 (Rational) —

c1r1 = this.cellContent(1,1)
v11 = when c1r1 = “” -999999 otherwise numericValue(c1r1)

Rational if: Matches a simple pattern OR the numeric value is ‘clean’

We use a trick: if it’s rational, it should equal itself when rounded to 10 places.

c1r1_isRat = p.number.matches(c1r1) or p.fraction(p.integer, p.integer).matches(c1r1) or (v11 = numericValue(“\operatorname{round}(${v11}, 10)”))

cellSuffix(1,1): when c1r1 = “” “” when c1r1_isRat check otherwise cross

— ROW 2, COL 1 (Rational) —

c1r2 = this.cellContent(2,1)
v12 = when c1r2 = “” -999999 otherwise numericValue(c1r2)
c1r2_isRat = p.number.matches(c1r2) or p.fraction(p.integer, p.integer).matches(c1r2) or (v12 = numericValue(“\operatorname{round}(${v12}, 10)”))

cellSuffix(2,1): when c1r2 = “” “” when c1r2_isRat check otherwise cross

— ROW 1, COL 2 (Irrational) —

c2r1 = this.cellContent(1,2)
v21 = when c2r1 = “” -999999 otherwise numericValue(c2r1)

Irrational if it is NOT rational and NOT empty and is a valid number

c2r1_isIrrat = not(p.number.matches(c2r1) or p.fraction(p.integer, p.integer).matches(c2r1) or (v21 = numericValue(“\operatorname{round}(${v21}, 10)”))) and not(c2r1 = “”) and v21 > -100000

cellSuffix(1,2): when c2r1 = “” “” when c2r1_isIrrat check otherwise cross

— ROW 2, COL 2 (Irrational) —

c2r2 = this.cellContent(2,2)
v22 = when c2r2 = “” -999999 otherwise numericValue(c2r2)
c2r2_isIrrat = not(p.number.matches(c2r2) or p.fraction(p.integer, p.integer).matches(c2r2) or (v22 = numericValue(“\operatorname{round}(${v22}, 10)”))) and not(c2r2 = “”) and v22 > -100000

cellSuffix(2,2): when c2r2 = “” “” when c2r2_isIrrat check otherwise cross

Dashboard Correctness

Correct = c1r1_isRat and c1r2_isRat and c2r1_isIrrat and c2r2_isIrrat
correct: Correct

Well… i mean that’s not literally true, but hey if it works in the Activity it works

1 Like

That relates a little to why I asked what the expected inputs would be. Fudging a little, but like you said if it works…