Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:06:40+00:00 2026-05-29T05:06:40+00:00

I ran this simple program, but when I convert from int to double ,

  • 0

I ran this simple program, but when I convert from int to double, the result is zero. The sqrt of the zeros then displays negative values. This is an example from an online tutorial so I’m not sure why this is happening. I tried in Windows and Unix.

/* Hello World program */

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

main()

{  int i;

   printf("\t Number \t\t Square Root of Number\n\n");

   for (i=0; i<=360; ++i)
        printf("\t %d \t\t\t %d \n",i, sqrt((double) i));


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:06:45+00:00Added an answer on May 29, 2026 at 5:06 am

    The problem is incorrect use of printf format – use %g/%f instead of %d

    BTW – if you are wondering what your code did here is some abridged explanation that may help you in understanding:

    printf routine has treated your floating point result of sqrt as integer. Signed, unsigned integers have their underlying bit representations (put simply – it’s the way how they are ‘encoded’ in memory, registers etc). By specifying format to printf you tell it how it should decipher that bit pattern in specific memory area/register (depends on calling conventions etc). For example:

    unsigned int myInt = 0xFFFFFFFF;
    printf( "as signed=[%i] as unsigned=[%u]\n", myInt, myInt );
    

    gives: “as signed=[-1] as unsigned=[4294967295]”

    One bit pattern used but treated as signed first and unsigned later. Same applies to your code. You’ve told printf to treat bit pattern that was used to ‘encode’ floating point result of sqrt as integer. See this:

    float myFloat = 8.0;
    printf( "%08X\n", *((unsigned int*)&myFloat) );
    

    prints: “41000000”

    According to single precision floating point encoding format.
    8.0 is simply (-1)^0*(1+fraction=0)*2^(exp=130-127)=2*3=8.0 but printed as int looks like just 41000000 (hex of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran this persons code. It worked as a main but when i put
How can you get 1 as an output from #2? #1 I ran this
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
I ran across this snippet in an internal web site, but I'm having trouble
I am running a very simple C++ program: #include <list> #include <vector> int main(int
I'm writing a simple program for file encryption. Mostly as an academic exercise but
I just ran this code, and it looks like nice language for me. But
Maybe there's no simple answer to this question, but I ask in case someone
Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do
I ran across this situation this afternoon, so I thought I'd ask what you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.