使用LUA实现 IEEE754算法

十六进制的HEX字符串 转换成 浮点数

function hexToFloat( hexString )
	if hexString == nil then
		return 0
	end
	local t = type( hexString )
	if t == "string" then
		hexString = tonumber(hexString , 16)
	end
 
	local hexNums = hexString
 
	local sign = math.modf(hexNums/(2^31))
 
	local exponent = hexNums % (2^31)
	exponent = math.modf(exponent/(2^23)) -127
 
	local mantissa = hexNums % (2^23)
 
	for i=1,23 do
		mantissa = mantissa / 2
	end
	mantissa = 1+mantissa
--	print(mantissa)
	local result = (-1)^sign * mantissa * 2^exponent
	result = tonumber(string.format('%.3f', result))
	return result
end

浮点数转换成 十六进制字符串

function floattohex(floatNum)
	if(floatNum == 0)then
		return 0;
	end
---给一个浮点数 33.758
--1.将浮点数分成整数和小数
	num_z,num_x= math.modf(floatNum/1)
	--print("整数:" .. num_z  .. "  小数:".. num_x)
--2.将整数部分化成二进制
	str_z = ""
	intercount = 0;  --转换成二进制一共多少位
	num1 = num_z
	if(num1 == 0)then
		intercount = 0
		str_z = "0"
	else
		repeat
		num = num1
		num1= math.modf(num1/2)
		num2 = num - num1*2
		intercount = intercount + 1  --阶乘
		str_z = str_z .. num2
		until (num1 == 0)
	end
	str_z = string.reverse(str_z)
	--print("整数:" .. str_z)
--3.将小数转换成二进制
	str_x = ""
	num2 = num_x
	repeat
	num2 = num2 *2
	num1,num2 = math.modf(num2/1)
	str_x = str_x .. num1
	until(num2 == 0 or #str_x >=40)
	--print("小数:"..str_x)
--4.浮点数的二进制表示
	--print("浮点数的二进制表示 :" .. str_z .. "."  .. str_x)

--浮点数的二进制表示 :一种:0.00110101110000101000111
                    --两种:1011.01101011100001010
--5.首先确认整数是否大于0,
        --(1)==0 右移  查找小数二进制何时有1
		--(2) >0 左移  
	
	e = 0
	str_m =""
	if(num_z == 0)then
		num_1 =string.find(str_x,"1")  --阶
		e = -num_1
		str_m = string.sub(str_x,num_1+1,-1)
	else
		e = intercount - 1
		str_m = string.sub(str_z,2,-1) .. str_x
	end	
	--??考虑str_m是否有23位
	if(#str_m ~= 23)then
		if(#str_m > 23)then
			str_m = string.sub(str_m,1,23)
		end
		if(#str_m <23)then
			len_m = 23 - #str_m
			for i = 1,len_m do
				str_m =  str_m .."0"
			end
		end
	
	end
	M = str_m
	--print("M = " .. M)    
	
	--考虑把它转换成二进制
	E = e + 127  
	str_e = ""  --E的二进制
	intercount_e = 0
	num1 = E
	repeat
		num = num1
		num1= math.modf(num1/2)
		num2 = num - num1*2
		intercount_e = intercount_e + 1  --阶乘
		str_e = str_e .. num2
	until (num1 == 0)
	
	if(#str_e ~= 8)then
		len_e = 8 - #str_e
		for i = 1,len_e do
			str_e =  str_e .."0"
		end
	end
	E = string.reverse(str_e)
	--print("E的二进制字符串:" .. E)--此时str_e 是E的二进制字符串
		
	
	
--6.确定 S
	if(floatNum > 0)then
		S = 0
	else	
		S = 1
	end
	--print("S:" .. S)
--7.将S E M 二进制转换
	str = S .. E .. M
	result = string.format("%08X",tonumber(str,2))
	
	--print ("result =" .. result)
	--print("**************************************************")
	return result;
end

用30000组数据测试数据的正确性

for i= 1,30000 do
	num = 0;
	--math.randomseed(os.time())
	num = tonumber(string.format('%.3f', math.random() + math.random(0,10) ))
 	num_result = num;
	print("i = " .. i.. " num :" .. num_result )
	str_hex = floattohex(num_result)
	num_str =hexToFloat(str_hex)
	if(num_result == tonumber(num_str))then
		--print("i = " .. i.. "  对比正确true")
	else
		print("对比错误false")
	end
	--print("**************************************************")
end
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Lua是一种脚本语言,它支持面向对象编程。在Lua中,我们可以通过表(table)来实现面向对象编程。表可以被看作是一个对象,它可以存储任意类型的数据(包括函数),还可以作为其他表的元素。下面是使用Lua实现面向对象的一些基本步骤: 1. 创建一个表,它代表一个类 2. 在这个表中定义一个构造函数,用于创建该类的实例 3. 在这个表中定义其他成员函数和成员变量 4. 在这个表中定义一个元表,用于实现继承和多态 下面是一个简单的例子,展示了如何使用Lua实现一个名为Person的类: ```lua -- 创建一个Person类 Person = {} -- 定义Person的构造函数 function Person:new(name, age) local obj = {} obj.name = name obj.age = age setmetatable(obj, self) self.__index = self return obj end -- 定义Person的成员函数 function Person:sayHello() print("Hello, my name is " .. self.name .. ", I'm " .. self.age .. " years old.") end -- 创建一个Person对象 local person = Person:new("Tom", 20) -- 调用对象的成员函数 person:sayHello() ``` 在这个例子中,我们首先创建了一个空表Person,它代表一个类。然后我们定义了Person的构造函数new,用于创建Person的实例。在构造函数中,我们创建了一个新表obj,并将name和age属性赋值给它。然后我们使用setmetatable函数将obj的元表设置为Person,并将self设置为__index。这样做可以让obj继承Person的成员函数。最后,我们返回obj。 接下来,我们定义了Person的成员函数sayHello,它用于打印出人的姓名和年龄。在最后,我们创建了一个Person对象person,并调用它的成员函数sayHello。 如果你想要继承Person类,你可以创建一个新的表Student,并将它的元表设置为Person。这样做可以让Student继承Person的成员函数和属性。如果你想要重写某个成员函数或属性,你可以直接在Student表中重新定义它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值