c# - Is this correct for calculating the nth root? -


for positive real numbers rth root given e^(ln(x)/r) negative real numbers if r odd rth root given -e^(ln|x|/r) if r real rth root of negative number not exist

static double rthroot (double r, double x) {     double y;      if (x > 0)     {         y = math.exp((math.log(x)) / r);     }      if (r+1 % 2 == 0)     {         if (x < 0)         {             y = -(math.exp((math.log(math.abs(x))) / r));         }     } }         

there 2 errors in code:

  1. you not returning value in rthroot. need return y oblige solve additional issue; call rthroot(2, -4). want return specific value (double.nan), want throw argumentexception or...?
  2. if (r + 1 % 2 == 0) not doing think doing. code equivalent if (r + (1 % 2) == 0) not want. correct code should if ((r + 1) % 2 == 0) or simpler , more readable if (r % 2 != 0) standard way check oddity.

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -