Converting int to double to output with decimals
Hello again! i got a little problem with converting int to double, i dont get it to output the decimals and i dont know where i do wrong ^^
Any tips?
75 Replies
what is ur expection and what is ur result?
nvm, i spotted the problem
double cel = (fahr - 32) * 5 / 9;
there it is
(fahr - 32) * 5 / 9
<-- all parts of this are integers, thus u get integer divisoncap5lut
REPL Result: Success
Result: int
Compile: 167.008ms | Execution: 20.851ms | React with β to remove this embed.
cap5lut
REPL Result: Success
Result: double
Compile: 336.144ms | Execution: 21.696ms | React with β to remove this embed.
cap5lut
REPL Result: Success
Result: double
Compile: 244.214ms | Execution: 22.982ms | React with β to remove this embed.
integer division truncates the stuff after the
.
to turn it into floating point division (either float
or double
)
one of the values has to be a floating point number type
u can either use double
notation (eg 1.5
), or float
notation (1.5f
), or cast to double or float (double)fahr
or (float)fahr
)
each of these will force the floating point division with double
s
((double)fahr - 32) * 5 / 9
(fahr - 32.0) * 5 / 9
(fahr - 32) * 5.0 / 9
(fahr - 32) * 5 / 9.0
Ohh, so i just need to put .0 after one of them and its fixed?
yep
because that will change the type of the value
Ahh, stupid me -.- didnt even think of that.... π€£ yea, now i get it.. haha, i've been staring me blind on the int argument and double ^^
thank you soooo much! π
but be aware that its more than just adding a
.0
anywhere
2 / 5 * 2.0
will result in 0
2.0 / 5 * 2
will result in 0.8so, if i add it to the first one it will result in 0 and the last one in 0.8?
or it depens on what numbers i have?
no it depends on where the division is
2 / 5 * 2.0
this is basically (2 / 5)
(which results in 0) mulitplied by 2.0
because of int divisionso in my code, this is right
And it looks like this:
it would be messed up? Or its just me that dont understand? ^^
im not really good at math tho..
naaah, thats messed up
the point is, for the division part of ur formula there it depends on the data types (int or double)
let me explain on this one:
2 / 5 * 2.0
u know that in math its "point calculation before line calculation" right?
as in multiplication and division have priority over addition and substractionlike, the first thing u calculate is inside ( ) then, i think * then / or the other way around..
as in
2 + 3 * 4 = 14
yea, there u start with 3 * 4 then + 2
yep
the
( )
have higher priority, so (2 + 3) * 4 = 20
exactly
so to get the double decimals right i always put the .0 somewhere after the division?
and now
1 * 2 / 3
, all have the same priority for the calculation
so it goes from left to rightyea,
the computer will first compute
1 * 2
and then will calculate 2 / 3
so 2 / 5 * 2.0
is the same as (2 / 5) * 2.0
at 2 / 5
both values are integers, thus it uses integer division
which results in 0
, then it calculates 0 * 2.0
, which is still 0
so to get rid of the integer division u have to change the type of either 2
or 5
in this example to get the expected 0.4
as resultoh, so if i understad this right, i have to put the .0 in eighter the first number of the division or the last? as long as it is a number that is dividing?
basically, ignore the numbers and just think about the types
int / int * double
-> (int / int) * double
int / int
(aka integer division) always results in another int
to get a floating point result u have to have (at least) one of these as floating point type
(i gonna smoke quick, brb, ~4-5min)yea, no problem, just trying to figure out this π
i feel so stupid because i dont understand π΅βπ«
is it the last thing thats get calculated that's supposed to have the decimal?
back
welcome back π
well, its really about the datatypes not the actual values
as soon as u have
int / int
it will result in an int
without the decimals
taking (fahr - 32) * 5 / 9.0
as example
thats
((fahr - 32) * 5) / 9.0
resolving that purely on data type:
((int - int) * int) / dobule
(int * int) / double
int / double
because not both are int
s at the division point, u get a double
result at the end
try to replace it like i did with these two
2 / 5 * 2.0
2.0 / 5 * 2
(do the first completely before starting the second)oh, so u calculate the integers first and lastly add the decimal to get decimal value?
in ur mind basically do it in 3 steps:
1) set parenthesis to visualize which will be computed first
2) replace all values with their types
3) resolve the resulting data type
if u have an integer type and a floating point type, no matter which mathematical operation u do, it will always end up with the floating point type
for
2 / 5 * 2.0
yes
because its going from left to right
do that step by step for 2 / 5 * 2.0
if u dont know, just guess.
if there is a mistake ill explain u what was wrong on that step2.0 / 5 * 2 = 0.8
and 2 / 5 * 2.0 = 0
yep
(not sure if u understood it, or if u scrolled up xD)
no actually i entered both in cisual studio and tried by them self ^^
so, did u understand it or not?
2 / 5 does result in a decimal value but the console is saying 0, and i guess thats becaus i dont have a decimal on either of them, so i get that decimal value from the 3rd? or im totaly out of grid.. feels like im talking values again -.- xD
damn, i feel so stupid xD
well, in normal real life what is
2 / 5
?0.4
less than 0.5 so it gets 0
and because its integer division u have to truncate the decimals, so 0.4 becomes what?
NO, not rounding, truncating! it will be just removed
ohπ
do u remember elementary school division? "the result is <blah> with a reminder of <blah2>"
integer division is just that, it calculates <blah>
hm, i think i need to read about math a little more... actually i dont remember.. so im gonna look it up and read.. damn, i feel so stupid... my memory sucks atm..
sorry for wasting your time by not fully understanding.. ^^
you have 5 apples and u want to give 2 people equally apples, how many apples does each one have?
i have to split them
oh
2 each
u dont have a knife, u cant split them
yep
thats integer division
Ahh okay π
that one i understood xD
i bet ur laughing you ass off right now π€£
i dont laugh at you!
its okay to not know/understand something.
u try to change that, so u have my respect π
its okay if u do, because i did xD
Thanksπ i really appriciate that, and your help! π
so, did it help or do u still have questions
(btw trust me, the more u learn, the more u learn that u dont understand sh_t :'D)
it helped with the integer division, but the double thing, if i only have integers and division is involved i add .0 in the division part?
haha thats true xD and the more i learn, the more i learn i dont understand and learn so i understand more xD
u have to make one of the division values a float/double, as soon as that happens u got ur knife and can slice through them all
well, having a knife is basically the wrong metaphor.
imagine u dont have "full" apples, but incredibly thin sliced apples
ahh so, if i got i right, whatever integers i have that gets divided, i add .0 to eighter one of the division values to get the float/double value?
thats how i understood it ^^
yep
thats also not just a division thing
1 + 2.0
makes it a double as well
basically no matter if addition, substraction, multiplication or division, as soon as a floating point value is involved, the result will be of that type
thats quite easy to explain looking at byte
and int
Okay then i understand the integer division part π
and 1 + 2.0 is the same as 1.0 + 2? or its the highest value that gets the decimal?
it doesnt matter which of the both it is, its a matter of if any of it is
that part is easier explainable with
int
and byte
so its basically division thats the part i have to look out for?
yeah
integer division can f_ck up ur math if u have floats/doubles involved
its a precision thingy, if 2 different number types are involved, the higher precision takes place
ohh, okay, i think i got it now then π
Damn, need same explenation as a 6 year old xD
yea, with the understanding i have now i get that the division is the big bully in math ^^
c
is here an int
, because its an byte
(0-255) and an int
(-2b to 2b roughly)
its not directly math, its about the different types
an integer simply cant represent 1/2
so u need to somehow "upgrade" the data type, so its representable. and that u do by making one of the operands that upgraded data type
ciggy time ;pintegers are integers, they dont have anything to do with the decimals until the division gets there, so its possible to add the decimal to whatever integer i want as long as division's not there? ^^
it depends on the result u want
if i want decimal result i add .0 and if i dont i dont add it? ^^
if u want sliced apples, slice one apple before dividing them
if u only want "complete" apples, dont slice them
slicing = converting to float/double
okay, time for an excercise!
3 / 2 * 2.0
, whats the result?and thats where .0 comes in to play, i add it where ever the result of eighter calculation gets decimal outcome?
2
because the .0 is not on the first 3 or 2 ?
to be correct, the result would be actually
2.0
in a sense, because its not an integer anymore, right?
because both 3
and 2
are integers, yes
there are 3 variants to make 3 / 2 * 2.0
result in 3.0
which are they?
(write the whole formula)oh, my adhd-brain is spinning too much right now xD
dont worry im stubborn ;p
just in words, what would u have to change to make it result in
3.0
?
(u can write the formulas instead ofc)haha i actually dont know xD
one is over after dividing 3 by to, then 1 * 3 is 3
3 by 2
haha
i need to take a little break atm, wifey is kind of hangry too so i have to get her to get food xD but i'll let u know when im back and we can keep going if u want ^^
uhmmm , its quite late for me
can we continue this in 6h or later?
i sorta want to sleep xD
absolutely xD we can do a little tomorrow if u want to ^^
thanks for all the help! i really appriciate it! π
well, u can continue without me, maybe someone else will step in
worst case u write some stuff and i will react to it after i woke up π
hahaha yea xD
and greet ur wife from that drunkard stranger on discord π
Hahaha, yea, i will xD
well, gotta go, thanks again for all the help! π
I think ya'll landed in a good spot! I tried my hand at writing up the rules here https://gist.github.com/John-Paul-R/40ef1b86a376f2838a3fa6fdab9c7cc7
Might be helpful, might not. Use it if its useful!
didnt even know about the
d
prefix for double
s π
but i think for a generic explanation its too much about actual values. as intro to describe the problem, yes
but its for example missing promotions like byte
to int
or float
to double
(basically in the same numberic system, just different precision)
(the whole thing sorta sounds like its just about integers vs floating points)
but generally speaking its a good start
and now i really hit the sack >.<yeah for the list of promotions, I tossed in an article describes all of them, figured I'd spare rewriting the thing, got long enough already, yknow?
C# numeric promotions
Color Of Code - website about software and programming