Floating Point Types Solutions - D Programming Language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Floating Point Types Solutions

#### Floating Point Types Dersi Problem Çözümleri

1. Replacing `float` with `double` produces an outcome that is surprising in a different way:

       // ...

           double result = 0;

       // ...

           if (result == 1) {
               writeln("As expected: 1");

           } else {
               writeln("DIFFERENT: ", result);
           }

   Although the value fails the `result == 1` comparison, it is still printed as 1:

       DIFFERENT: 1

   That surprising outcome is related to the way floating point values are formatted for printing. A more accurate approximation of the value can be seen when the value is printed with more digits after the decimal point. (We will see formatted output in a later chapter.):

               writefln("DIFFERENT: %.20f", result);

       DIFFERENT: 1.00000000000000066613

2. Replacing the three `int`s with three `double`s is sufficient:

               double first;
               double second;

               // ...

               double result;

3. The following program demonstrates how much more complicated it would become if more than five variables were needed:

       import std.stdio;

       void main() {
           double value_1;
           double value_2;
           double value_3;
           double value_4;
           double value_5;

           write("Value 1: ");
           readf(" %s", &value_1);
           write("Value 2: ");
           readf(" %s", &value_2);
           write("Value 3: ");
           readf(" %s", &value_3);
           write("Value 4: ");
           readf(" %s", &value_4);
           write("Value 5: ");
           readf(" %s", &value_5);

           writeln("Twice the values:");
           writeln(value_1 * 2);
           writeln(value_2 * 2);
           writeln(value_3 * 2);
           writeln(value_4 * 2);
           writeln(value_5 * 2);

           writeln("One fifth the values:");
           writeln(value_1 / 5);
           writeln(value_2 / 5);
           writeln(value_3 / 5);
           writeln(value_4 / 5);
           writeln(value_5 / 5);
       }

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Wed Aug 19 13:28:52 2026