Skip to content Skip to sidebar Skip to footer

How to Read in a Real Floating Point Variable

Coding superpowers

Behavior as Usual

My mood was much amend afterward visiting the basement. In this dark and grim world, I finally had at least one hope to agree onto - the hope that there was at least a chance that I could brand it dorsum to my own time.
For a few days later on getting into the basement and back out again, I engaged in some people watching whenever I was moving around the base of operations. Past the looks of it, no one had particularly noticed my absence, which meant that I should be able to continue helping Noname to restore his modules without drawing suspicion.

Jessica was heading toward me in the corridor. "Hi, Teo. You look and then enthusiastic today!"
"Thanks, you await pretty chipper yourself!" I returned. I suddenly noticed that everyone other than me was headed in the other direction. "Where'southward everyone going?" I asked.
"Ritchie returned from an expedition and said he had something to testify off. He called a meeting in the large kitchen at..." Jessica looked at her watch, "Now! Let's go!" I turned on my heels and tried to keep upwardly.

The Types

Ritchie was standing at the front of the kitchen, and near of the tables were full. People were however trickling in niggling past little, and anybody was waiting for him to begin. Finally:
"Hullo, everyone! Thank you lot all for coming on such curt notice. I've just returned from the automobile city, and an marry of ours shared some intel they've been working on obtaining for a while. It turns out in that location are many more types than just bool , int , and string .

"More? Really?" Someone from the crowd chosen. Ritchie replied, "Yes, many more than. There are so-called congenital-in types , which are more complex types than cord and the other types nosotros already know. Let's start simple and get over some of the built-in types, the offset of which are the float and double types. Both of these types represent a real number, sometimes referred to equally a floating-point number (this is where the name 'float' goes from). For example," at this, Ritchie uncapped a marker and began writing on the whiteboard backside him, "numbers like 2.5, 17.9456, and 0.seven are existent numbers."

"Why would we need such complexity?" someone asked, mirroring my own thoughts. "I apply int in all my programs and everything seems to work fine!"

Ritchie had conspicuously predictable this question. "In our earth, the principal currency is viruses. Every number of viruses is an integer number. You can accept 2, 5, or 10 viruses, simply non 2.5, correct? That'due south why y'all recollect in terms of the int blazon. Before our time though, people had a currency - the dollar - to calculate their money. Y'all could have 1.l dollars, or i.63 dollars, not just one or 2. In a similar manner, you can use a double variable to store a real number inside:"

                                    double                        myMoney            =            187.543            ;                  

He continued, "I know information technology seems complicated, but you tin add, subtract, divide, and multiply double variables aforementioned every bit you exercise with integers."

                                    double                        myMoney            =            10.25            ;                          double                        fourTimesMyMoney            =            10.25            *            4.0            ;            Console.            WriteLine            (fourTimesMyMoney)            ;            // Outputs 41                          double                        decreasedMoney            =            myMoney            -            1.25            ;            Console.            WriteLine            (decreasedMoney)            ;            // Outputs 9                          double                        dividedMoney            =            decreasedMymoney            /            2            ;            Panel.            WriteLine            (dividedMoney)            ;            // Outputs iv.5                          double                        addToMyMoney            =            dividedMoney            +            100.34            ;            Console.            WriteLine            (addToMyMoney)            ;            // Outputs 104.84                  

Ritchie continued making the case for the new types. "Fifty-fifty better," he said, "you can add together, subtract, divide, and multiply double and int variables together. The result is automatically converted to a double blazon."

                                    double                        someDouble            =            seven.five            ;                          int                        someInt            =            2            ;                          double                        sum            =            someDouble            +            someInt;            Console.            WriteLine            (sum)            ;            // Outputs ix.5                          double                        subtract            =            someDouble            -            someInt;            Panel.            WriteLine            (subtract)            ;            // Outputs 5.5                          double                        multiply            =            someDouble            *            someInt;            Console.            WriteLine            (multiply)            ;            // Outputs fifteen                          double                        divide            =            someDouble            /            someInt;            Panel.            WriteLine            (split)            ;            // Outputs three.75                  

This isn't whatever different than how you lot do things exterior of coding. Nosotros tin add integers and existent numbers, subtract them, divide them, and multiply them without any hiccups. At the aforementioned fourth dimension, yous cannot multiply strings with any other type, or divide or subtract. Y'all can only add strings (using "+") to numeric types . Here are some examples:

                                    double                        someDouble            =            seven.5            ;                          int                        someInt            =            2            ;                          string                        someString            =            "Hello "            ;                          double                        result1            =            someDouble            +            someInt;            // Ok - adding double and int                          string                        result2            =            someString            +            someDouble;            // Ok - suspend double to string                          string                        result3            =            someString            +            someInt;            // Ok - append int to string                          string                        result4            =            someString            *            someInt;            // NOT ok - can't multiply int and            // string                          string                        result5            =            someString            /            someDouble;            // Non ok - can't divide string on            // double                          string                        result6            =            someString            *            someString;            // Non ok - tin can't multiply strings                  

To output a double to the screen or concatenate it with a cord, you lot use exactly the aforementioned mechanisms equally you used with an int .

                                    double                        temperature            =            17.5            ;            Console.            WriteLine            (temperature)            ;            // Outputs: 17.5            Console.            WriteLine            (            "Temp is "            +            temperature)            ;            // Outputs: Temp is 17.5            Console.            WriteLine            (                          $"Temp is                                            {                temperature                }                            "                        )            ;            // Outputs: Temp is 17.5                  

Everyone in the room was nodding. Ritchie asked u.s.a. to attempt creating a double variable and so output it to the screen.

If you want to read a double from the screen, employ the method double .Parse(...) the aforementioned as you used int .Parse(...). Here is an instance:

                                    string                        numberString            =            Console.            ReadLine            (            )            ;                          double                        number            =            double            .            Parse            (numberString)            ;                  

Round hatch

"What about the float type?" asked someone behind me.

A float is nearly the same equally a double . The main divergence is that a double is twice equally large as a float in retentivity space (hence the name 'double'). This results in a different range of values that yous can store in these two types:

Type Infinite in Memory Approximate Range Precision Default Value Example
float 4 bytes -three.iv x 10 38 to +3.four x 10 38 vii 0.0 f 0.1234567 f
double 8 bytes ±5.0 ten x -324 to ±1.7 x ten 308 16 0.0 0.1234567890123456

By default, every real number in C# programme is treated as a double . To force C# to recognize a number as a float, you must add together an "f" at the end of every float, as shown in the above table and the beneath case.

You can add, decrease, multiply, and dissever double and float variables the same as you tin with doubles and ints. The result will over again be a double type.

                                    double                        someDouble            =            1.five            ;                          float                        someFloat            =            2.5f            ;                          double                        sum            =            someDouble            +            someFloat;            Console.            WriteLine            (sum)            ;            // Outputs iv                          double                        subtract            =            someDouble            -            someFloat;            Panel.            WriteLine            (decrease)            ;            // Outputs -1                          double                        multiply            =            someDouble            *            someFloat;            Console.            WriteLine            (multiply)            ;            // Outputs 3.75                          double                        split up            =            someDouble            /            someFloat;            Console.            WriteLine            (divide)            ;            // Outputs 0.6                  

After walking effectually to make sure we had grasped the new concepts, Ritchie returned to the forepart of the room. "From now on," he began, "we're going to use the double blazon because it's as fast every bit the float type, but provides much more than precise results. I suggest that you all get comfortable using the double in your programs, and only use a float if you have a very skilful reason to do and then."

"Let's wrap up the first part of our meeting," Ritche said. "Complete this next job then accept a half-hour lunch break. We'll get into more new types this afternoon!"

Programmer table

brownthentlen.blogspot.com

Source: https://codeasy.net/lesson/double_type_introduction

Post a Comment for "How to Read in a Real Floating Point Variable"