简体   繁体   English

OCaml中的IEEE 64和32位浮点验证

[英]IEEE 64 and 32 bit float validation in OCaml

I have a string matching the following regex \\-?[0-9]*\\.[0-9]+ which supposedly represents a IEEE floating point number. 我有一个与以下正则表达式\\-?[0-9]*\\.[0-9]+相匹配的字符串,该表达式可能表示IEEE浮点数。 It could be single or double precision and I know the type in advance. 它可以是单精度或双精度,我事先知道类型。 I need to check if it could be interpreted as a valid value in given precission. 我需要检查在给定的精度下是否可以将其解释为有效值。 Something like: 就像是:

val is_valid_float: string -> bool
val is_valid_double: string -> bool

For double precision numbers, I can just parse it using float_of_string and catch the exception. 对于双精度数字,我可以使用float_of_string解析它并捕获异常。 I am unsure how to deal with single precision. 我不确定如何处理单精度。

@JonathanChan's comments are enlightening, probably more so than anything I might say. @JonathanChan的评论很有启发性,可能比我能说的要多。

However, I'm not even sure what you mean by validation. 但是,我什至不知道验证的含义。

Is "1.0000000000000001" a valid float? “ 1.0000000000000001”是有效的浮点数吗?

 val f : string = "1.0000000000000001"
 # float_of_string f;;
 - : float = 1.
 #

There's no exception to indicate that this number can't be represented as distinct from 1.0. 毫无例外地表示此数字不能表示为不同于1.0。

If you ignore issues of precision, it might not be difficult to test against the representable range just as a string operation. 如果您忽略精度问题,那么就可以像字符串操作一样在可表示范围内进行测试,这可能并不困难。

As @JonathanChan points out, the best answer probably depends on how sure you need to be (and what you want to be sure of, exactly). 正如@JonathanChan指出的那样,最好的答案可能取决于您需要被确保的程度(以及您想要确定的确切程度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 32位,64位和80位浮点IEEE-754的可表示值范围? - Range of representable values of 32-bit, 64-bit and 80-bit float IEEE-754? binary32 float(IEEE)中的前导位 - leading bit in binary32 float(IEEE) 在仅支持 32 位浮点数的平台上,如何将 IEEE754 64 位双精度数除以 1000? - How could you divide an IEEE754 64-bit double by 1000 on a platform only supporting 32-bit float? 将32位浮点型转换为IEEE 80位 - Converting 32-bit float to IEEE 80-bit 将 IEEE 32 位浮点数 [1:2) 的范围映射到任意 [a:b) - map range of IEEE 32bit float [1:2) to some arbitrary [a:b) 如何判断数字是否可以精确表示为32位IEEE浮点数? - How to tell if a number is exactly representable as a 32-bit IEEE float? 如何在C ++中将十六进制转换为IEEE 754 32位浮点 - How to convert Hex to IEEE 754 32 bit float in C++ 如何使用32位浮点IEEE 754计算浮点数值以存储在内存中? - How to calculate the float number value to store in memory using IEEE 754, 32 bit float? 如何将float转换为64bit二进制或将32bit float转换为64bit int-PHP - how convert float to 64bit binary or 32bit float to 64bit int - php 如何在Perl中将四个字符转换为32位IEEE-754浮点数? - How can I convert four characters into a 32-bit IEEE-754 float in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM