cencodingfloating-pointintegerieee-754

Why do I get large random values when using scanf("%f", &intVar)? How do float inputs get converted to these values?


I know I used the wrong format specifier in my C program when reading floats into int variables using scanf("%f", &variable).

I don't know how the program yields large random values. How do the input values get converted to these random values?

#include <stdio.h>
#include <stdlib.h>

// Width Height --> Floating Point

float findArea (float width, float height)
{
  float area;
  area = width* height;
  printf("width:%f\n",width);
  printf("height:%f\n",height);
  return area;
}


int main()
{
  int heightRectangle, widthRectangle;
  float area;
  printf("Enter width: ");
  scanf("%f",&widthRectangle);
  printf("Enter height: ");
  scanf("%f", &heightRectangle);
  area = findArea(widthRectangle,heightRectangle);
  printf("The area of your given rectangle is: %f \n",area);

  return 0;
}

Output:

Enter width: 5.0
Enter height: 2.5
width:1084227584.000000
height:1075838976.000000
The area of your given rectangle is: 1166454293721513984.000000 

Size of int and float in my system is 4 bytes. I used sizeof operator to determine that.

Compiler Information:

I am using online compiler programiz

Guys, once again I know I used wrong format specifier.I'm just expecting answer which explains how the values 5.0 and 2.5 are getting converted to 1084227584 and 1075838976.


Solution

  • Why this Behaviour? Floating numbers with float data types are encoded using IEEE-754 encoding standard (single precision format).

    How does IEEE-754 work in detail?

    1.What is IEEE 754?
    > It's a standard for how computers represent floating-point numbers. Both positive and negative 
    floating-point numbers.
    
    2.Single Precision Basics
    >Single precision means we use 32 bits (or 4 bytes) to represent a number.
    
    3.The 32 Bits are Split Into Three Parts:
    
    >Sign bit: 1 bit that shows if the number is positive or negative (0 for positive, 1 for negative).
    >Exponent: 8 bits that help represent the range of the number.
    >Mantissa (or Fraction): 23 bits that hold the actual digits of the number.
    
    4.The Formula for the Number:
    
    >The number is represented as: 
        [±]1.[mantissa]*2^(exponent-127)
     
    >The exponent is stored with a bias of 127, which helps in handling both small and large numbers.
    

    For more information about single precision format refer this

    refer:->iee-754

    Explanation:=

    width=5.0->101.0 height=2.5->10.1

    Single precision conversion of width and height
    width=0 10000001 01000000000000000000000
    Height=0 10000000 01000000000000000000000
    

    Both int and float data types use 32-bit / 4bytes of computer memory to store data.

    These numbers were readed as float but interpreted as integers. Normal integer 32-bit conversion:

    Width =01000000101000000000000000000000 is 1084227584
    Height=01000000001000000000000000000000 is 1075838976
    

    width*height=1166454293721513984