C++ sqrt function returning truncated (wrong) output [duplicate]












-1















This question already has an answer here:




  • Integer division always zero [duplicate]

    1 answer




I have a doubt on the function sqrt of math.h



I was debugging some code, and I figured out that my function was not working properly, as the square root of 2/3 was always returning zero.



I tried to isolate the problem by just writing down some square root calculations in a separate file, and the function isn't returning the correct values.



Am I missing something?



#include <math.h>
#include <iostream>

using namespace std;
int main(){
cout << sqrt(2/3) << endl;
cout << sqrt(16/2) << endl;
cout << sqrt(9/2) << endl;
return 0;

}


This is the output I receive:



0
2.82843
2


When the correct output should be:



0.81650
2.82843
2.12132


Thank you in advance.










share|improve this question













marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    There's a dup somewhere...
    – YSC
    Nov 12 '18 at 17:15










  • @YSC if you can find it, that would be really helpful. Couldn't find anything beside someone saying that sqrt(3) was evalued as zero in gdb
    – Michele Fattoruso
    Nov 12 '18 at 17:17










  • @YSC more like +Inf dupes, to keep in spirit with IEEE 754
    – SergeyA
    Nov 12 '18 at 17:17








  • 1




    Does stackoverflow.com/questions/9764677/… answers your question?
    – YSC
    Nov 12 '18 at 17:17








  • 2




    @MicheleFattoruso there is nothing to be ashamed of. The intricacies of arithmetic are one of the most counter-intuitive parts of C/C++.
    – SergeyA
    Nov 12 '18 at 17:22
















-1















This question already has an answer here:




  • Integer division always zero [duplicate]

    1 answer




I have a doubt on the function sqrt of math.h



I was debugging some code, and I figured out that my function was not working properly, as the square root of 2/3 was always returning zero.



I tried to isolate the problem by just writing down some square root calculations in a separate file, and the function isn't returning the correct values.



Am I missing something?



#include <math.h>
#include <iostream>

using namespace std;
int main(){
cout << sqrt(2/3) << endl;
cout << sqrt(16/2) << endl;
cout << sqrt(9/2) << endl;
return 0;

}


This is the output I receive:



0
2.82843
2


When the correct output should be:



0.81650
2.82843
2.12132


Thank you in advance.










share|improve this question













marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    There's a dup somewhere...
    – YSC
    Nov 12 '18 at 17:15










  • @YSC if you can find it, that would be really helpful. Couldn't find anything beside someone saying that sqrt(3) was evalued as zero in gdb
    – Michele Fattoruso
    Nov 12 '18 at 17:17










  • @YSC more like +Inf dupes, to keep in spirit with IEEE 754
    – SergeyA
    Nov 12 '18 at 17:17








  • 1




    Does stackoverflow.com/questions/9764677/… answers your question?
    – YSC
    Nov 12 '18 at 17:17








  • 2




    @MicheleFattoruso there is nothing to be ashamed of. The intricacies of arithmetic are one of the most counter-intuitive parts of C/C++.
    – SergeyA
    Nov 12 '18 at 17:22














-1












-1








-1








This question already has an answer here:




  • Integer division always zero [duplicate]

    1 answer




I have a doubt on the function sqrt of math.h



I was debugging some code, and I figured out that my function was not working properly, as the square root of 2/3 was always returning zero.



I tried to isolate the problem by just writing down some square root calculations in a separate file, and the function isn't returning the correct values.



Am I missing something?



#include <math.h>
#include <iostream>

using namespace std;
int main(){
cout << sqrt(2/3) << endl;
cout << sqrt(16/2) << endl;
cout << sqrt(9/2) << endl;
return 0;

}


This is the output I receive:



0
2.82843
2


When the correct output should be:



0.81650
2.82843
2.12132


Thank you in advance.










share|improve this question














This question already has an answer here:




  • Integer division always zero [duplicate]

    1 answer




I have a doubt on the function sqrt of math.h



I was debugging some code, and I figured out that my function was not working properly, as the square root of 2/3 was always returning zero.



I tried to isolate the problem by just writing down some square root calculations in a separate file, and the function isn't returning the correct values.



Am I missing something?



#include <math.h>
#include <iostream>

using namespace std;
int main(){
cout << sqrt(2/3) << endl;
cout << sqrt(16/2) << endl;
cout << sqrt(9/2) << endl;
return 0;

}


This is the output I receive:



0
2.82843
2


When the correct output should be:



0.81650
2.82843
2.12132


Thank you in advance.





This question already has an answer here:




  • Integer division always zero [duplicate]

    1 answer








c++ math.h






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 17:14









Michele Fattoruso

3315




3315




marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    There's a dup somewhere...
    – YSC
    Nov 12 '18 at 17:15










  • @YSC if you can find it, that would be really helpful. Couldn't find anything beside someone saying that sqrt(3) was evalued as zero in gdb
    – Michele Fattoruso
    Nov 12 '18 at 17:17










  • @YSC more like +Inf dupes, to keep in spirit with IEEE 754
    – SergeyA
    Nov 12 '18 at 17:17








  • 1




    Does stackoverflow.com/questions/9764677/… answers your question?
    – YSC
    Nov 12 '18 at 17:17








  • 2




    @MicheleFattoruso there is nothing to be ashamed of. The intricacies of arithmetic are one of the most counter-intuitive parts of C/C++.
    – SergeyA
    Nov 12 '18 at 17:22














  • 2




    There's a dup somewhere...
    – YSC
    Nov 12 '18 at 17:15










  • @YSC if you can find it, that would be really helpful. Couldn't find anything beside someone saying that sqrt(3) was evalued as zero in gdb
    – Michele Fattoruso
    Nov 12 '18 at 17:17










  • @YSC more like +Inf dupes, to keep in spirit with IEEE 754
    – SergeyA
    Nov 12 '18 at 17:17








  • 1




    Does stackoverflow.com/questions/9764677/… answers your question?
    – YSC
    Nov 12 '18 at 17:17








  • 2




    @MicheleFattoruso there is nothing to be ashamed of. The intricacies of arithmetic are one of the most counter-intuitive parts of C/C++.
    – SergeyA
    Nov 12 '18 at 17:22








2




2




There's a dup somewhere...
– YSC
Nov 12 '18 at 17:15




There's a dup somewhere...
– YSC
Nov 12 '18 at 17:15












@YSC if you can find it, that would be really helpful. Couldn't find anything beside someone saying that sqrt(3) was evalued as zero in gdb
– Michele Fattoruso
Nov 12 '18 at 17:17




@YSC if you can find it, that would be really helpful. Couldn't find anything beside someone saying that sqrt(3) was evalued as zero in gdb
– Michele Fattoruso
Nov 12 '18 at 17:17












@YSC more like +Inf dupes, to keep in spirit with IEEE 754
– SergeyA
Nov 12 '18 at 17:17






@YSC more like +Inf dupes, to keep in spirit with IEEE 754
– SergeyA
Nov 12 '18 at 17:17






1




1




Does stackoverflow.com/questions/9764677/… answers your question?
– YSC
Nov 12 '18 at 17:17






Does stackoverflow.com/questions/9764677/… answers your question?
– YSC
Nov 12 '18 at 17:17






2




2




@MicheleFattoruso there is nothing to be ashamed of. The intricacies of arithmetic are one of the most counter-intuitive parts of C/C++.
– SergeyA
Nov 12 '18 at 17:22




@MicheleFattoruso there is nothing to be ashamed of. The intricacies of arithmetic are one of the most counter-intuitive parts of C/C++.
– SergeyA
Nov 12 '18 at 17:22












1 Answer
1






active

oldest

votes


















3














1,2,4,8,9 are integers constants, and the arithmetic result of integers will always be an integer.



Therefore, you should work with double constants, so you should try:



cout << sqrt(2.0/3.0) << endl;
cout << sqrt(16.0/2.0) << endl;
cout << sqrt(9.0/2.0) << endl;





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    1,2,4,8,9 are integers constants, and the arithmetic result of integers will always be an integer.



    Therefore, you should work with double constants, so you should try:



    cout << sqrt(2.0/3.0) << endl;
    cout << sqrt(16.0/2.0) << endl;
    cout << sqrt(9.0/2.0) << endl;





    share|improve this answer


























      3














      1,2,4,8,9 are integers constants, and the arithmetic result of integers will always be an integer.



      Therefore, you should work with double constants, so you should try:



      cout << sqrt(2.0/3.0) << endl;
      cout << sqrt(16.0/2.0) << endl;
      cout << sqrt(9.0/2.0) << endl;





      share|improve this answer
























        3












        3








        3






        1,2,4,8,9 are integers constants, and the arithmetic result of integers will always be an integer.



        Therefore, you should work with double constants, so you should try:



        cout << sqrt(2.0/3.0) << endl;
        cout << sqrt(16.0/2.0) << endl;
        cout << sqrt(9.0/2.0) << endl;





        share|improve this answer












        1,2,4,8,9 are integers constants, and the arithmetic result of integers will always be an integer.



        Therefore, you should work with double constants, so you should try:



        cout << sqrt(2.0/3.0) << endl;
        cout << sqrt(16.0/2.0) << endl;
        cout << sqrt(9.0/2.0) << endl;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 17:19









        SHR

        5,73562341




        5,73562341















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues