• if square_root takes a uint64_t argument then it should return a uint32_t
  • x = (x + (s / x)) / 2; This update of the test value uses integer divisions which means it is not guaranteed to converge (due to the truncation moving your approximation further away than the previous iteration). You should also check that x > 0 to avoid accidental division by zero.
  • it looks like seed is trying to calculate pow(10, log_100(x)) in some way, but it has bugs, for example a < 10 is true for radicand values of 100-999, 10000-99999, 1000000-9999999 etc. which probably isn’t what you want. An easier way to estimate a starting value for the first iteration would be to take a number that has half as many bits as the input, since we know that if a number X uses K bits then its square root will use K/2 bits. You can use a method to calculate the position of the most significant set bit in the input and then use 2^(k/2) as the seed.
  • for ease of readability and checking correctness, I would recommend that you don’t use implicit casts to float for arithmetic and avoid implicit casts back to integers with the results. Declare floating point variables when you want floating point arithmetic and then use explicit casts back to ints to make it clear to the reader (reviewer) that any truncation of the result is intentional. This is considered good practice when writing C. You can compile with -Wall -Werror (with gcc) to enforce those checks and make them compile errors that you must fix.
@velox_vulnus@lemmy.ml
creator
link
fedilink
1
edit-2
7d

deleted by creator

General Programming Discussion
!programming@lemmy.ml

    A general programming discussion community.

    Rules:

    1. Be civil.
    2. Please start discussions that spark conversation

    Other communities

    Systems

    Functional Programming

    Also related

    • 0 users online
    • 5 users / day
    • 14 users / week
    • 44 users / month
    • 138 users / 6 months
    • 1 subscriber
    • 336 Posts
    • 544 Comments
    • Modlog