General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Datatypes
  3. Primitive Datatypes

Floating point

The floating-point data type in computer programming is used to represent real numbers, which can include fractions and decimal points, and can cover a very wide range of values. Floating-point numbers are essential for representing non-integer numbers, especially when dealing with scientific calculations, graphics, real-world measurements, and anywhere precision with fractional numbers is required.

Characteristics of the Floating-Point Data Type:

  • Representation: Floating-point numbers are represented using a scientific notation-like system in binary, consisting of a sign (positive or negative), a significand (or mantissa), and an exponent. This allows for the representation of very large numbers, very small numbers, and fractions.

  • Precision: The precision of a floating-point number, which is how accurately the number can represent real values, depends on the amount of memory allocated to it. Common floating-point types include float (single precision) and double (double precision), with double offering higher precision.

  • IEEE 754 Standard: Most modern computer systems follow the IEEE 754 standard for floating-point arithmetic, which defines the format for representing floating-point numbers and the behavior of various arithmetic operations.

  • Range and Accuracy: Floating-point numbers have a much wider range than integer types, but they can introduce rounding errors in calculations due to their finite precision. This is a key consideration in numerical computing and requires careful handling to avoid significant accuracy issues.

Usage in Different Programming Languages:

  • C/C++: Defines float (typically 32 bits) and double (typically 64 bits), with an optional long double for extended precision.

  • Java: Has float (32 bits) and double (64 bits) as its floating-point types, with double being the default for floating-point numbers.

  • Python: Uses float for floating-point numbers, which is implemented as double precision in CPython.

Example Usage:

# Python example
distance = 103.4  # A floating-point number
velocity = 2.5
time = distance / velocity  # Floating-point arithmetic
print("Time:", time)

In this Python example, distance and velocity are floating-point numbers used to calculate time, which is also a floating-point number. The program outputs the time, demonstrating a basic arithmetic operation with floating-point numbers.

PreviousBooleanNextNothing (Null)

Last updated 1 year ago

Was this helpful?