Thread: C++: is there a function more faster than sqrt()?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    446

    C++: is there a function more faster than sqrt()?

    C++: is there a function more faster than sqrt()?
    i did several searchs and i found some.. but some don't give me some results and others are slow

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    37
    Square root calculations are never cheap. You could enable fast math for your compiler which breaks IEEE compliance though. E.g. it will be assumed that everything is finite.
    Another question worth to ask is why does it even make such a big difference for you how fast sqrt() is? We don't know your code. So we can't judge whether there's an opportunity to reduce the number of sqrt() calls. Some times it is possible to just do all the operations with the square values and calculate the root only once at the end.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,047
    16 clock cycles isn't fast enough?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,434
    Quote Originally Posted by flp1969 View Post
    16 clock cycles isn't fast enough?
    When they do dumb stuff like trying to draw each pixel with a new thread, then they'd find a way to make an infinite machine run slowly.
    how use multithread inside a class? - C++ Forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    248
    Quote Originally Posted by joaquim View Post
    C++: is there a function more faster than sqrt()?
    i did several searchs and i found some.. but some don't give me some results and others are slow
    Not sure if this would help in your particular case, but sometimes it is practical to "redo the maths" of your program to process the squares instead. I used that very approach in a low-level circle-drawing routine I implemented some years back. First I stored the square of the circle radius in a variable. Then, as I looped through a grid of candidate pixels, I only needed to compare x*x + y*y with the precomputed radius_squared. I did need a sqrt() call for certain edge cases (I needed sub-pixel accuracy there) but regardless the entire routine ran much much faster than before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sqrt function
    By kbpsu in forum C++ Programming
    Replies: 16
    Last Post: 04-02-2009, 11:32 PM
  2. problems with sqrt function
    By edzsmith in forum C Programming
    Replies: 7
    Last Post: 01-01-2008, 02:56 PM
  3. sqrt() function help
    By willc0de4food in forum C Programming
    Replies: 5
    Last Post: 03-14-2005, 09:07 PM
  4. Sqrt() Function
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 04-22-2003, 04:31 PM
  5. sqrt function
    By Extol in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2003, 06:04 AM

Tags for this Thread