Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Display not showing correct value.....

Status
Not open for further replies.

neelam29

New Member
hi,

i'm trying to display result of a simple math calcultation on 5-digit 7-segment display.
my display code is absouletly correct. but problem is in math equation.

I'm using 89c52 controller and doing coding in Keil C51.

here's the part of code which isn't working properly:


unsigned long int result;

result = 16.80*100;


the display shows 1679 whereas it should be 1680.

i tried :


unsigned long int result;

result = (unsigned long int)(16.80*100);


but sill the same result.

how can i solve the problem?

thanks in advance,

regards,
Neelam
 
If that is your actual code (which I suspect it isn't) then

result=1680;

will work.

I suspect that you have variables involved and this is a result of rounding errors caused by using floating point. With more information it may be possible to advise something like,

result = (unsigned long int)((16.80+0.005)*100);

Mike.
 
Thanks a lot Mike for replying.

My actual code surely involves a variable.

my actual code is


/////////////////////////////////////////////////////////////////


x=x+0.90; //where x is float type variable.starting from x=15.00

result = x*100;

display(result);


/////////////////////////////////////////////////////////////////




now display shows:-

1500
1590
1679
1769
1859
1949
2039
2129
2219
2309
2399
2489
2579
2669
2759
2849
2939
3029
3119
3209
3299
3389
3480 //error vanished here automatically
3570
3660
3750
3840
3930
4020
4110
4200
4290
4380
4470
4560
4650
4740
4830
.
.
.
.
so on....




but even if i remove that variable x and i make the equation as result = 16.80 *100 then also the result is 1679
 
Did you try,

result = (unsigned long int)((x+0.005)*100);

You could of course do,

x=x+0.905; //where x is float type variable.starting from x=15.00
result = x*100;


Or, combine the two calculations and reorder them to,

result=x*100+90;

The earlier you can convert to integer the more accurate the result will be.

Mike.
 
Just realised you are doing (15.0+X*0.9)*100. You can rearrange this and do (1500+(int)X*90) and your error should disappear.

Mike.
 
Thanks a lot MIKE!!!!!!!

with your method the problem is solved in starting but when the counter reaches somewhere around 79080 it shows 79081 here the error adds up by 1.

i'think still some problem is left....

Regards
 
Thanks a lot MIKE!!!!!!!

with your method the problem is solved in starting but when the counter reaches somewhere around 79080 it shows 79081 here the error adds up by 1.

i'think still some problem is left....

Regards
 
It will probably work fine as,

result = (unsigned long int)((x+0.001)*100);

But rearranging would be a better solution.

Code:
for(x=15;x<1000;x++){
    Result=1500+x*90;
    display(result);
}

Mike.
 
My computer dealer told me that after the 486 Intel called their next chip Pentium because when they tried to call it 586, they kept getting 585.9999999999
 
There was a bug in the early pentium processors but it was fixed a long time ago.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top