Better Any-root Function in C.

Author: 7000series

Posts

Total: 10
7000series
7000series's avatar
Debates: 21
Posts: 192
1
3
8
7000series's avatar
7000series
1
3
8
For those of you that don't/won't include basic math libraries in your programs, you probably do something like this for your root functions:
double sqrt( double whole ) {
     double accumulator = 1.0;
     double difference = whole - 1.0;
     for( int i = 0; i < 54; ++i ) {
          difference /= 2.0;
          if( whole < SQUARE( accumulator ) == whole < SQUARE( accumulator + difference ) ) {
               accumulator += difference;
          }
     }
     return accumulator;
}
Yucky. Objects are cool, so join the crowd. Here is an example program, which shows how you can just shove all the computation up front.
#define ROOTSET 2 #include <stdlib.h>double power( double base, long exponent ) { double scraper = 1.0; while( exponent != 1 ) { if( exponent % 2 == 1 ) { scraper *= base; } base *= base; exponent /= 2; } return base * scraper;}typedef struct { double spacing; long tablesize; double * table;} RootTable;RootTable roottable_init( long mode, double spacing, long tablesize ) { RootTable retval; retval . spacing = power( spacing, mode ); retval . tablesize = tablesize; retval . table = malloc( sizeof( double ) * tablesize ); long index = 0; for( long i = 0; ; ++i ) { for( long u = 0; u < power( i + 1, mode ) - power( i, mode ); ++u ) { retval . table[ index ] = spacing * ( ( double )( i ) + ( double )( u ) / ( double )( power( i + 1, mode ) - power( i, mode ) ) ); if( ++index >= tablesize ) { return retval; } } }}#define ISPOS( X ) ( X + X > X )double roottable_get( RootTable self, double searchval ) { if( ISPOS( searchval ) && ( long )( searchval / self . spacing ) < self . tablesize ) { return self . table[ ( long )( searchval / self . spacing ) ]; } else { return 0.0; }}void roottable_free( RootTable self ) { free( self . table );}#include <stdio.h>int main() { double spacing; long tablesize; scanf( "%lf %ld", & spacing, & tablesize ); RootTable subject = roottable_init( ROOTSET, spacing, tablesize ); while( 1 ) { double searchval; if( scanf( "%lf", & searchval ) == 1 ) { printf( "%lf\n", roottable_get( subject, searchval ) ); } else { roottable_free( subject ); printf( "End Program\n" ); return 0; } }}
Only braindead apes use functional programming in the big 25. Look at how smart I am, using objects in my C code.
TheGreatSunGod
TheGreatSunGod's avatar
Debates: 0
Posts: 182
1
2
5
TheGreatSunGod's avatar
TheGreatSunGod
1
2
5
-->
@7000series
Programming is the most useful science, especially after it gave us AI to replace programmers.
ADreamOfLiberty
ADreamOfLiberty's avatar
Debates: 0
Posts: 4,363
3
2
2
ADreamOfLiberty's avatar
ADreamOfLiberty
3
2
2
-->
@7000series
This is some special form of barbarism.
FLRW
FLRW's avatar
Debates: 0
Posts: 7,420
3
4
8
FLRW's avatar
FLRW
3
4
8

Shila
Shila's avatar
Debates: 0
Posts: 6,791
3
3
5
Shila's avatar
Shila
3
3
5
-->
@7000series
Only braindead apes use functional programming in the big 25. Look at how smart I am, using objects in my C code.
It says in your profile you are still in high school.
FLRW
FLRW's avatar
Debates: 0
Posts: 7,420
3
4
8
FLRW's avatar
FLRW
3
4
8
-->
@Shila

No, it says his education is high school.
7000series
7000series's avatar
Debates: 21
Posts: 192
1
3
8
7000series's avatar
7000series
1
3
8
-->
@Shila
The year is 25, not my age
Dr.Franklin
Dr.Franklin's avatar
Debates: 32
Posts: 10,753
4
7
11
Dr.Franklin's avatar
Dr.Franklin
4
7
11
Stop with this C propaganda
7000series
7000series's avatar
Debates: 21
Posts: 192
1
3
8
7000series's avatar
7000series
1
3
8
-->
@Dr.Franklin
Stop with this C propaganda
There are two hidden premises here. . .

Hidden Premise 1 : My post is C propaganda.
UNTRUE - - - > Yes, my post contains code written in C, but does that really mean that my post is C propaganda?

Hidden Premise 2 : All propaganda must be stopped.
UNTRUE - - - > What if I had a propaganda poster encouraging kids to eat nutritious diets?

Now, If I made a debate titled "C is the best programming language", would you accept?
Shila
Shila's avatar
Debates: 0
Posts: 6,791
3
3
5
Shila's avatar
Shila
3
3
5
-->
@FLRW
No, it says his education is high school.
So his education stopped after high school.