#program doesnt print precise decimal numbers

16 messages · Page 1 of 1 (latest)

sudden jewel
#

practicing for my test tomorrow using this pythagoras calculator, however everytime the last one or two digits wouldnt round up properly


#include <stdio.h>
#include <math.h>

int main () {
    
    int adj = 0;
    int opp = 0;
    double hyp = 0.0;
    

    
    printf("Enter the length of the adjacent side: \n");
    scanf("%d", &adj);
    
    printf("Enter the length of the opposite side: \n\n");
    scanf("%d", &opp);
    

    hyp = sqrt(adj*adj + opp*opp);
    printf("The hypotenuse is %.6f in length.\n\n", hyp);
    
    
    printf("          +\n");
    printf("          |\\\n");
    printf("          | \\\n");
    printf(" %.6f|  \\  %.6f\n", (double)opp, hyp);
    printf("          |   \\\n");
    printf("          |    \\\n");
    printf("          +-----+ \n");
    printf("          %.6f\n", (double)adj);

    printf("\nNote: Right-angle triangle is not drawn to scale!\n");
    
    return 0;
      
}```
gritty pythonBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sudden jewel
tall forum
#

floating point numbers be that way sometimes

#

its impossible for them to be exact

#

kinda silly that the question requires so many decimals

#

although I'd have expected double to have that amount of precision

#

try float (and related functions) instead to see what happens :D

wind moat
#

or change the rounding mode in your floating point environment

#

or maybe the tasks didn't want you to use floating point numbers in the first place, but expected you to approximate the fraction

#

ok weird, the expected results is actually

36.400549446402591355486512457635163968888348412882387191890908956
#

if you use float hyp it should work

gritty pythonBOT
#

This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

sudden jewel
#

!solved