![]() |
Forum Index : Microcontroller and PC projects : Divide by Zero Error?
Author | Message | ||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Can't understand how I'd get that error occasionally on this line of code that I'm using in the IEEE conversion for reading Modbus Energy meters. [Code][82] Ans!=((Reading% Or &h800000) And &hffffff)/2^(23-Expo%)*(-1)^Sign% 'Extract Mantissa & Add implied 24th bit, Shift Point in Binary & apply sign Error: Divide by zero > [/code} 2^0=1, so not that. -1^0=1 & -1^1=-1. It is probably corresponding to the meters display showing 0 Watts. Any Suggestions? Phil. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Split the line up a bit x! = 2^(23-Expo%) y! = (-1)^Sign% z! = ((Reading% Or &h800000) And &hffffff) print x!, y!, z! Ans! = z/x*y It might show you where the problem is. You might be getting caught with integers rounding to zero. Jim VK7JH MMedit |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Thanks Jim, Good thought. 2^?? might become to small to be a non-zero value of ?? gets too big in the negative direction. Phil. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
You were spot Jim, Trapped it with this. [Code]Response:- Exponent: -127Sign: : 0 [86] Ans!=((Reading% Or &h800000) And &hffffff)/2^(23-Expo%)*(-1)^Sign% 'Extract Mantissa & Add implied 24th bit, Shift Point in Binary & apply sign Error: Divide by zero > [/code] And the Dos Version proves this. [Code]> print 2^-127 5.877471754e-39 > i%=2^-127 > print i% 0 >[/code] Phil. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
DOS MMBasic Ver 5.04.09 Beta 11 Copyright 2011-2018 Geoff Graham n Float Integer 10 1024 1024 9 512 512 8 256 256 7 128 128 6 64 64 5 32 32 4 16 16 3 8 8 2 4 4 1 2 2 0 1 1 -1 0.5 1 -2 0.25 0 -3 0.125 0 -4 0.0625 0 -5 0.03125 0 -6 0.015625 0 -7 0.0078125 0 -8 0.00390625 0 -9 0.001953125 0 -10 0.0009765625 0 > When the conversion from Integer to Float happens varies between programming languages and often bites you on the bum. VK7JH MMedit |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Hmmm, Wonder how to best patch my code as at the moment I'm not using a 1 then the mantissa after the decimal with the exponent added. Instaead I'm putting the one on the front to make a whole number integer & dividing that by the shifted exponent shifted to get the floating value. It's reporting correct values for all the ranges I've tested, but will obviously fall over with a number that is divided by less than / 2^-2. Phil. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
I suppose the question is at what point in my function should I switch from itegers to floats. Can't do the binary math on the floats, so would need to be after that. Phil. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Create 3 local variables as floats and do the maths in stages as in my previous post. x! = 2^(23-Expo%) y! = (-1)^Sign% z! = ((Reading% Or &h800000) And &hffffff) ' print x!, y!, z! Ans! = z/x*y Jim VK7JH MMedit |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |