TCL:变量存储一个代表浮点数的十六进制值,如何将它作为浮点数显示在屏幕上? [英] TCL: A variable stores a hex value that represents a floating point number, how can this be printed on screen as a float?
问题描述
0x3f800000在单精度浮动数学中表示1.0。我尝试了此操作,但无法从程序中获得正确的结果:
The 0x3f800000 represents 1.0 in single precision floating maths. I tried this but could not get the correct result from the program:
set x 0x3f800000
set y [expr double($x)]
puts $y
我只想投射值x转换为浮点数,因此它将以浮点数的形式显示在屏幕上。
I just want to "cast" the value of x into a float so it will print on screen as float. How do I do this in tcl?
请注意,在我要解决的原始问题中,tcl脚本从Quartus II系统中的硬件寄存器读取值控制台调试系统。但是,我给出了一个简单的示例代码,以使其他人容易理解我需要做什么。
Please note that in the original problem that I am trying to solve, a tcl script reads value from a hardware register in Quartus II System Console debug system. However, I have given a simple example code to make it easy for others to understand what I need to do.
推荐答案
您可以通过将整数转换为4字节的二进制字符串,然后将二进制字符串转换为浮点数来获得浮点值:
You can obtain the floating point value by converting the integer to a 4-byte binary string and then converting the binary string to a float:
set x 0x3f800000
binary scan [binary format i $x] f y
puts $y
这篇关于TCL:变量存储一个代表浮点数的十六进制值,如何将它作为浮点数显示在屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!