square-root

sqrt of uint64_t vs. int64_t

有些话、适合烂在心里 提交于 2020-06-27 07:26:54
问题 I noticed that calculating the integer part of square root of uint64_t is much more complicated than of int64_t . Please, does anybody have an explanation for this? Why is it seemingly much more difficult to deal with one extra bit? The following: int64_t sqrt_int(int64_t a) { return sqrt(a); } compiles with clang 5.0 and -mfpmath=sse -msse3 -Wall -O3 to sqrt_int(long): # @sqrt_int(long) cvtsi2sd xmm0, rdi sqrtsd xmm0, xmm0 cvttsd2si rax, xmm0 ret But the following: uint64_t sqrt_int(uint64_t

sqrt of uint64_t vs. int64_t

孤街醉人 提交于 2020-06-27 07:26:23
问题 I noticed that calculating the integer part of square root of uint64_t is much more complicated than of int64_t . Please, does anybody have an explanation for this? Why is it seemingly much more difficult to deal with one extra bit? The following: int64_t sqrt_int(int64_t a) { return sqrt(a); } compiles with clang 5.0 and -mfpmath=sse -msse3 -Wall -O3 to sqrt_int(long): # @sqrt_int(long) cvtsi2sd xmm0, rdi sqrtsd xmm0, xmm0 cvttsd2si rax, xmm0 ret But the following: uint64_t sqrt_int(uint64_t

RMS calculation DC offset

﹥>﹥吖頭↗ 提交于 2020-05-17 08:49:19
问题 I need to implement an RMS calculations of sine wave in MCU (microcontroller, resource constrained). MCU lacks FPU (floating point unit), so I would prefer to stay in integer realm. Captures are discrete via 10 bit ADC. Looking for a solution, I've found this great solution here by Edgar Bonet: https://stackoverflow.com/a/28812301/8264292 Seems like it completely suits my needs. But I have some questions. Input are mains 230 VAC, 50 Hz. It's transformed & offset by hardware means to become 0

C++ Square Root Function Bug

。_饼干妹妹 提交于 2020-01-15 04:51:09
问题 I have a c++ algorithm that calculates the square root of an integer. The program works with the exception of a single flaw. It is unable to calculate the square root of a number that is below 1. For example, it cant calculate the square root of .5 or .9 or .0000001 etc. but works as planned for all other situations. I have X set so it doesn't allow a negative input, but I still can't see why it wont return a value for anything less than 1. include <iostream> #include <cmath> #include

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

China☆狼群 提交于 2020-01-13 07:01:08
问题 I'm trying to learn algorithms and coding stuff by scratch. I wrote a function that will find square roots of square numbers only, but I need to know how to improve its performance and possibly return square roots of non square numbers function squareroot(number) { var number; for (var i = number; i >= 1; i--) { if (i * i === number) { number = i; break; } } return number; } alert(squareroot(64)) Will return 8 Most importantly I need to know how to improve this performance. I don't really

Print a square root symbol (√) in java [duplicate]

爱⌒轻易说出口 提交于 2020-01-11 11:36:29
问题 This question already has answers here : square root character/symbol (3 answers) Closed 6 years ago . I am just wondering how do you print a square root(√) character in Java? I am assuming you use its unicode or something? 回答1: Simply System.out.println("Square Root: \u221A"); Source: first match on Google. 回答2: Here's the unicode number for it: http://www.fileformat.info/info/unicode/char/221a/index.htm And this prints it out to a file for me: package com.sandbox; import org.apache.commons

Finding a square root using only integers

好久不见. 提交于 2020-01-05 00:50:06
问题 Recently, I came across a problem in someone's programming class. It asked them to compute a square root using only integers; they were to use one integer to represent the part before the decimal point and another integer to represent the part after the decimal point. The problem said that using floating point numbers was not allowed. However, after thinking about it for some time, I can't seem to come up with a way of doing it without using floating point. I've Googled high and low and I can

Easiest way to find Square Root in Swift?

百般思念 提交于 2020-01-01 07:37:19
问题 I have been trying to figure out how to programmatically find a square root of a number in Swift. I am looking for the simplest possible way to accomplish with as little code needed. I now this is probably fairly easy to accomplish, but can't figure out a way to do it. Any input or suggestions would be greatly appreciated. Thanks in advance 回答1: In Swift 3, the FloatingPoint protocol appears to have a squareRoot() method. Both Float and Double conform to the FloatingPoint protocol. So: let x

Why do we only check up to the square root of a prime number to determine if it is prime? Can't we use cube root?

限于喜欢 提交于 2019-12-26 16:31:11
问题 If a number n can be written as axb and m=sqrt(n). Here n=m*m. We say we only need to check upto m because min(a,b)<=m. So cant we take cube roots? Suppose we take n=21, then n=1x3x7. But Cube root is 2. Why does this method fail? 回答1: Consider n = 143 = 11 * 13. The cube root of 143 is between 5 and 6. If you only test divisibility by the primes up to 6, you will not find either of the two factors of n and will mistakenly conclude that 143 is prime. 来源: https://stackoverflow.com/questions