合理解析科学记数法? [英] Parsing scientific notation sensibly?
问题描述
我希望能够编写一个函数,该函数接收一个以科学记数法表示的数字作为字符串,并将系数和指数作为单独的项目从中分离出来.我可以只使用正则表达式,但传入的数字可能未规范化,我更希望能够规范化然后将部分分解.
一位同事已经获得了使用 VB6 的部分解决方案,但还没有完成,如下面的文字记录所示.
cliVe>一 = 1e6克莱夫>?系数:" &o.spt(a) &" 指数: " &o.ept(a)系数:10 指数:5
应该是 1 和 6
cliVe>a = 1.1e6克莱夫>?系数:" &o.spt(a) &" 指数: " &o.ept(a)系数:1.1 指数:6
正确
cliVe>a = 123345.6e-7克莱夫>?系数:" &o.spt(a) &" 指数: " &o.ept(a)系数:1.233456 指数:-2
正确
cliVe>a = -123345.6e-7克莱夫>?系数:" &o.spt(a) &" 指数: " &o.ept(a)系数:1.233456 指数:-2
应该是 -1.233456 和 -2
cliVe>a = -123345.6e+7克莱夫>?系数:" &o.spt(a) &" 指数: " &o.ept(a)系数:1.233456 指数:12
正确
有什么想法吗?顺便说一下,Clive 是基于 VBScript 的 CLI,可以在我的
(来源:json.org)
如果您查看他的 json2.js的第 456 行a> 脚本(在 javascript 中安全转换为 JSON 或从 JSON 安全转换),您将看到正则表达式的这一部分:
/-?d+(?:.d*)?(?:[eE][+-]?d+)?/
讽刺的是,这与他的语法图不匹配......(看起来我应该提交一个错误)我相信一个实现该语法图的正则表达式是这样的:
/-?(?:0|[1-9]d*)(?:.d+)?(?:[eE][+-]?d+)?/
如果您还想允许初始 +,您会得到:
/[+-]?(?:0|[1-9]d*)(?:.d+)?(?:[eE][+-]?d+)?/
根据自己的喜好添加捕获括号.
我还强烈建议您充实一堆测试用例,以确保包含您想要包含(或不包含)的那些可能性,例如:
允许:+33.2e23-4.70e+9-.2E-4-7.6603不允许:+0003(前导零)37.e88(e 前的点)
祝你好运!
I want to be able to write a function which receives a number in scientific notation as a string and splits out of it the coefficient and the exponent as separate items. I could just use a regular expression, but the incoming number may not be normalised and I'd prefer to be able to normalise and then break the parts out.
A colleague has got part way of an solution using VB6 but it's not quite there, as the transcript below shows.
cliVe> a = 1e6
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 10 exponent: 5
should have been 1 and 6
cliVe> a = 1.1e6
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.1 exponent: 6
correct
cliVe> a = 123345.6e-7
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.233456 exponent: -2
correct
cliVe> a = -123345.6e-7
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.233456 exponent: -2
should be -1.233456 and -2
cliVe> a = -123345.6e+7
cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
coeff: 1.233456 exponent: 12
correct
Any ideas? By the way, Clive is a CLI based on VBScript and can be found on my weblog.
Google on "scientific notation regexp" shows a number of matches, including this one (don't use it!!!!) which uses
*** warning: questionable ***
/[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?/
which includes cases such as -.5e7 and +00000e33 (both of which you may not want to allow).
Instead, I would highly recommend you use the syntax on Doug Crockford's JSON website which explicitly documents what constitutes a number in JSON. Here's the corresponding syntax diagram taken from that page:
(source: json.org)
If you look at line 456 of his json2.js script (safe conversion to/from JSON in javascript), you'll see this portion of a regexp:
/-?d+(?:.d*)?(?:[eE][+-]?d+)?/
which, ironically, doesn't match his syntax diagram.... (looks like I should file a bug) I believe a regexp that does implement that syntax diagram is this one:
/-?(?:0|[1-9]d*)(?:.d+)?(?:[eE][+-]?d+)?/
and if you want to allow an initial + as well, you get:
/[+-]?(?:0|[1-9]d*)(?:.d+)?(?:[eE][+-]?d+)?/
Add capturing parentheses to your liking.
I would also highly recommend you flesh out a bunch of test cases, to ensure you include those possibilities you want to include (or not include), such as:
allowed:
+3
3.2e23
-4.70e+9
-.2E-4
-7.6603
not allowed:
+0003 (leading zeros)
37.e88 (dot before the e)
Good luck!
这篇关于合理解析科学记数法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!