-10

When I try to get the square root of a periodic decimal number the result is 0.

Example code:

sqrt(4/99) 

The accuracy not important, it is okay to truncate some digits.

Murphy
  • 3,827
  • 4
  • 21
  • 35
Joel
  • 9
  • 1
  • What exactly is the problem? Can you share some **working** code to demonstrate it? – Nico Haase Mar 29 '18 at 09:15
  • 3
    Dupe of https://stackoverflow.com/questions/9455271/integer-division-always-zero – Rakete1111 Mar 29 '18 at 09:19
  • These questions pop up from time to time. But what source of learning are all those new programmers using? Basic arithmetic and its quirks is usually covered early in a [decent book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Mar 29 '18 at 09:39
  • how to get square root of a periodic decimal point? sqrt() method outputs zero – Joel Mar 29 '18 at 10:40

1 Answers1

8

You used an integer as input to the sqrt() function.

#include <cmath>
#include <iostream>

int main(int argc, char** argv)
{
    std::cout << std::sqrt(4 / 99) << std::endl;
    std::cout << std::sqrt(4.0 / 99) << std::endl;
}

Output
0
0.201008
schorsch_76
  • 794
  • 5
  • 19