Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I will freely admit not  being computing math guru, but this one got me stomped. 
I just cannot see what I am  doing wrong. 

Code 
float hypotenuse;
				hypotenuse = 1.0 - .1;			// OK
				printf("\nhypotenuse of  circle %i %f", (int) R + 1,
						hypotenuse);

				hypotenuse = 1.0 - (float) radius; // wrong result 
				printf("\nhypotenuse of  circle %i %f", (int) R + 1,
						hypotenuse);

Results 
radius  difference between  base and circle 0 0.100000
hypotenuse of  circle 1 0.900000            OK 
hypotenuse of  circle 1 1.090909            why not 0.9 ?? 


Text copied from LibreOffice.

What I have tried:

No, I did not ask Mrs Google and RTFM already.
Posted
Updated 18-Apr-20 6:52am
Comments
Patrice T 17-Apr-20 13:45pm    
Post a piece of code that we can run.
What is the value of radius ?
phil.o 17-Apr-20 17:07pm    
A circle doesn't have a hypotenuse; a right triangle does.
Richard MacCutchan 18-Apr-20 4:12am    
What are the values of R and radius? And what on earth is the hypotenuse of a circle?
KarstenK 18-Apr-20 4:12am    
never compute in printf statement. Use a variable, there are enough of it :-O

If I dummy in the variables you don't show defintions or values for:
C++
#include <stdio.h>

int main()
    {
    float hypotenuse;
    float radius =  0.1;
    int R = 666;
    hypotenuse = 1.0 - .1;			// OK
    printf("\nhypotenuse of  circle %i %f", (int) R + 1,
    		hypotenuse);
    
    hypotenuse = 1.0 - (float) radius; // wrong result 
    printf("\nhypotenuse of  circle %i %f", (int) R + 1,
    		hypotenuse);
    
    return 0;
    }
I get what I expected:
hypotenuse of  circle 667 0.900000                                                                 
hypotenuse of  circle 667 0.900000                                                                 
                                                                                                   
...Program finished with exit code 0
So i think you need to look at the content of your radius using the debugger for your system. I suspect it's not what you think it is...
 
Share this answer
 
Before some police gets uptight , I suggest to move this to real discussion forum.


I am sorry, due to some technical difficulties with local internet provider part of the post got missed.
I generally check inputs, to prevent GIGO.

So to get back to solution and skip all the superfluous fluff - if I add

radius = .1;

as indicated - all is well.

????


This is for those who wanted to see the code.

C++
	// radius difference
				float radius_new  = (float) IMG_circle[1].radius - (float) IMG_circle[0].radius;
				printf("\nradius  differece between  base and cicrcle %i %f", R,
						radius_new);
				float hypotenuse;
				float hypotenuse_new;
				//radius = .1;
				hypotenuse = 1.0 - .1;			//- (double) radius;
				//printf("\nhypotenuse of  cicrcle %i %f", (int) R,
				//		hypotenuse);
// BUG
				float base = 1.0;
				hypotenuse_new = base - (float) radius_new; //  (float) radius;
				hypotenuse_new = base -( (float) IMG_circle[1].radius - (float) IMG_circle[0].radius);
				printf("\nBase circle radius     %f ",(float) IMG_circle[0].radius);
				printf("\nCurrent circle radius  %f ",(float) IMG_circle[1].radius);
				printf("\ndiff (hypotenuse_new)  %f ",(float) IMG_circle[1].radius - (float) IMG_circle[0].radius);
				printf("\ndiff (hypotenuse_new)  %f ",(float) IMG_circle[0].radius - (float) IMG_circle[1].radius);
				printf("\nnew hypotenuse of  circle of radius %f %i %f", radius_new, (int) R,
						(float) hypotenuse_new);


And here is current output

radius  differece between  base and cicrcle 0 0.100000
Base circle radius     1.000000 
Current circle radius  0.909091 
diff (hypotenuse_new)  -0.090909 
diff (hypotenuse_new)  0.090909 
new hypotenuse of  circle of radius -0.090909 0 1.090909
hypotenuse of  cicrcle 1 0.810000


Plase note that the first printf prints POSITIVE radius - which I assumed was correct.
Using SAME values the "current circle radius" CHANGED.

I am really not sure where is my error and how to solve it.
And that is just beginning of the calculations...


C++
// radius difference
            float radius = IMG_circle[1].radius - IMG_circle[0].radius;
            printf("\nradius  differece between  base and cicrcle %i %f", R,
                    radius);
            float hypotenuse;
            hypotenuse = 1.0 - .1;          //- (double) radius;
            printf("\nhypotenuse of  cicrcle %i %f", (int) R + 1,
                    hypotenuse);

                           radius = .1;

            hypotenuse = 1.0 - (float) radius;
            printf("\nhypotenuse of  cicrcle %i %f", (int) R + 1,
                    hypotenuse);


radius  differece between  base and cicrcle 0 0.100000
hypotenuse of  cicrcle 1 0.900000
hypotenuse of  cicrcle 1 1.090909
 
Share this answer
 
v2
Comments
Richard MacCutchan 18-Apr-20 10:30am    
What is R, and what exactly are you trying to do? All i can see is that you are printing the difference between 1.0 and 0.1. And you should know by now the inherent problems of accuracy with floating point numbers.
Would it be appropriate to ask to remove / delete this thread?
It now longer matters HERE.
Moved to real discussion forum.
 
Share this answer
 
Comments
CHill60 20-Jul-20 12:38pm    
You can remove your own question which will delete the entire thread. Hover over your post and click the icon that looks like a dustbin
Vaclav_ 20-Jul-20 22:11pm    
Sorry, no dustbin in original post, just in "Solutions". Do not want to do something upsetting to the forum by deleting them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900