Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:19 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Divide by Zero Error?

Author Message
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 02:40am 09 Apr 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 6283
Posted: 02:57am 09 Apr 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 1667
Posted: 03:39am 09 Apr 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 1667
Posted: 04:24am 09 Apr 2018
Copy link to clipboard 
Print this post

You were spot Jim,

Trapped it with this.

  Quote  Function Bin2Flt(ResStr As String) As Float

IeeeStr$=
"&H"+Mid$(ResStr, 7,8)
'Print IeeeStr$

Reading%=
Val(IeeeStr$)
Sign%=Reading% >>
31 'Read the Sign Bit
Expo%=((Reading% >> 23) And &hff) - 127 'Extract Exponent, Remove sign, Subtract Offset

If 2^(23-Expo%)*(-1)^Sign% = 0 Then
Print "Exponent: ";Expo%; " Sign: "; Sign%
End If
Ans!=((Reading%
Or &h800000) And &hffffff)/2^(23-Expo%)*(-1)^Sign% 'Extract Mantissa & Add implied 24th bit, Shift Point in Binary & apply sign
Bin2Flt=Ans!



[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.




Edited by Phil23 2018-04-10
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 04:33am 09 Apr 2018
Copy link to clipboard 
Print this post

  Quote   PRINT "n","Float","Integer"
FOR n = 10 TO -10 STEP -1
x! =
2^n
y% =
2^n
PRINT n, x!, y%
NEXT n


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: Australia
Posts: 1667
Posted: 05:47am 09 Apr 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 1667
Posted: 05:53am 09 Apr 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 6283
Posted: 06:35am 09 Apr 2018
Copy link to clipboard 
Print this post

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025