C++ floating point data types [5]

Sdílet
Vložit
  • čas přidán 7. 06. 2023
  • Learn about the floating point data types and how to use them to correctly store real numbers in this C++ tutorial for beginners using Visual Studio 2022 C++ .
    // Learn more:
    CH02: Introduction to C++ ⇒ • CH02: Introduction to C++
    // Join the new Programming for Everyone Community Forum:
    professorhank.freeforums.net/
    // Consider supporting this channel in multiple ways
    paypal.me/hankstalica1
    Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB

Komentáře • 2

  • @PauseBoden
    @PauseBoden Před 9 měsíci +1

    I love your videos, thank you! I'm a bit confused about something in this video though. At around 7:46 you say that 3.6 is stored as a double but it's being truncated to a float. Isn't 3.6 a float? I guess I don't understand why, if 3.6 is within the range of float and f has been defined as a floating-point data type variable, the value 3.6 wouldn't be stored as float?

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 9 měsíci +2

      Literals are stored in memory as doubles by C++. That's 8 bytes. A float variable is 4 bytes. So if you assign 3.6 to a float variable, 4 bytes get cut off. Now, in this case, who cares because 3.6 doesn't require all 8 bytes to store correctly. However, if you stored something like a bunch of digits for PI, then you might lose some of those decimal places when you lose the 4 bytes.