Skip to content

Commit

Permalink
feat-fix: handle Infinity, NaN in value conversion (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce authored Feb 26, 2024
2 parents b840f09 + 53e492b commit 3cc5277
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/r-bridge/lang-4.x/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export function ts2r<T>(value: T): string {
} else if(typeof value === 'string') {
return JSON.stringify(value)
} else if(typeof value === 'number') {
if(isNaN(value)) {
return RNa
} else if(!isFinite(value)) {
return RInf
}
return value.toString()
} else if(typeof value === 'boolean') {
return value ? 'TRUE' : 'FALSE'
Expand Down Expand Up @@ -87,6 +92,14 @@ export function number2ts(value: string): RNumberValue {
}
}

if(value === RNa) {
return {
num: NaN,
complexNumber,
markedAsInt
}
}

const floatHex = lcValue.match(RNumHexFloatRegex)
if(floatHex == null) {
return {
Expand Down
6 changes: 6 additions & 0 deletions test/functionality/r-bridge/lang/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ describe('Bidirectional Value Translation', () => {
assert.equal(ts2r(1), '1')
assert.equal(ts2r(1.1), '1.1')
})
it('Infinity and NaN', () => {
assert.equal(ts2r(Infinity), 'Inf')
assert.equal(ts2r(10 ** 1000), 'Inf')
assert.equal(ts2r(NaN), 'NA')
assert.equal(ts2r(Math.sqrt(-1)), 'NA')
})
it('strings', () => {
assert.equal(ts2r(''), '""', 'empty string')
assert.equal(ts2r('abc'), '"abc"')
Expand Down

4 comments on commit 3cc5277

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"artificial" Benchmark Suite

Benchmark suite Current: 3cc5277 Previous: d69018e Ratio
Total per-file 1708.1640781818182 ms (4372.140351450595) 1511.494083 ms (3708.3225553463017) 1.13
Retrieve AST from R code 324.9143401818182 ms (280.351087245084) 64.46248863636363 ms (125.66414120100016) 5.04
Normalize R AST 35.0996325 ms (69.38499942854659) 94.99519236363636 ms (152.9376581920758) 0.37
Produce dataflow information 71.72562854545454 ms (179.03385968027578) 65.2556795909091 ms (167.18441854609554) 1.10
Total per-slice 1.9223473846245347 ms (1.349367787530074) 1.8724288794806876 ms (1.3873679811565907) 1.03
Static slicing 1.399701092003241 ms (1.2485198100128643) 1.4074784311593942 ms (1.3118563756339259) 0.99
Reconstruct code 0.5079115782595336 ms (0.31750637215742356) 0.4524929302663976 ms (0.22636683004337768) 1.12
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.7329390759026896 # 0.7329390759026896 # 1
reduction (normalized tokens) 0.7209834969577295 # 0.720988345209971 # 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark '"artificial" Benchmark Suite'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3cc5277 Previous: d69018e Ratio
Retrieve AST from R code 324.9143401818182 ms (280.351087245084) 64.46248863636363 ms (125.66414120100016) 5.04

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"social-science" Benchmark Suite

Benchmark suite Current: 3cc5277 Previous: d69018e Ratio
Total per-file 3859.3413677 ms (6423.307301708781) 3566.86774236 ms (5920.286185213901) 1.08
Retrieve AST from R code 329.30045494 ms (128.465045123108) 72.22227936 ms (60.97026629229811) 4.56
Normalize R AST 35.56690112 ms (31.70492821365286) 113.02594858 ms (70.71306906384982) 0.31
Produce dataflow information 192.74878253999998 ms (296.982259211564) 163.44175874 ms (276.9623037407309) 1.18
Total per-slice 9.418868334228062 ms (15.223906127466295) 8.599255365044066 ms (14.312877376595168) 1.10
Static slicing 8.817350936211136 ms (15.082635326950875) 8.071953766135923 ms (14.188089279803133) 1.09
Reconstruct code 0.5915658703182898 ms (0.3128741470605588) 0.5187709959800451 ms (0.27627204677573897) 1.14
failed to reconstruct/re-parse 9 # 9 # 1
times hit threshold 967 # 967 # 1
reduction (characters) 0.8935817303062393 # 0.898713819973478 # 0.99
reduction (normalized tokens) 0.8531248144961374 # 0.8579790415512589 # 0.99

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark '"social-science" Benchmark Suite'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3cc5277 Previous: d69018e Ratio
Retrieve AST from R code 329.30045494 ms (128.465045123108) 72.22227936 ms (60.97026629229811) 4.56

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.