Fixed point square root function wrong results for big numbers - c

Hello friends and enemies
I have this square root function from a library called libfixmath which works great, however from 32767.0f and above it starts returning wrong and negative results. The numbers I need square root of are rather big, up to 999999.0f. Any of you know what I could do to fix the problem?
#include <iostream>
float into_float(const int value) {
return ((float)value / 65536.0f);
}
int from_float(const float value) {
return (int)(value * 65536.0f);
}
int fp_sqrt(int value) {
unsigned char neg = (value < 0);
unsigned int num = (neg ? -value : value);
unsigned int result = 0;
unsigned int bit;
unsigned char n;
if (num & 0xFFF00000) {
bit = (unsigned int)1 << 30;
} else {
bit = (unsigned int)1 << 18;
}
while (bit > num) bit >>= 2;
for (n = 0; n < 2; n++) {
while (bit) {
if (num >= result + bit) {
num -= result + bit;
result = (result >> 1) + bit;
} else {
result = (result >> 1);
}
bit >>= 2;
}
if (n == 0) {
if (num > 65535) {
num -= result;
num = (num << 16) - 0x8000;
result = (result << 16) + 0x8000;
} else {
num <<= 16;
result <<= 16;
}
bit = 1 << 14;
}
}
if (num > result) {
result++;
}
return (neg ? -result : result);
}
void main() {
float flt_value = 32767.0f;
int int_value = from_float(flt_value);
float flt_root = sqrt(flt_value);
int int_root = fp_sqrt(int_value);
float flt_root2 = into_float(int_root);
printf("sqrt: %f fp_sqrt: %f", flt_root, flt_root2);
getchar();
}
Thank you leppie, I changed some stuff around and it works with big numbers now, I am not sure if what I did is totally right though but here it is:
#include <iostream>
float into_float(const int value) {
return ((float)value / 65536.0f);
}
long long from_float(const float value) {
return (long long)(value * 65536.0f);
}
long long fp_sqrt(long long value) {
unsigned char neg = (value < 0);
long long num = (neg ? -value : value);
long long result = 0;
long long bit;
unsigned char n;
if (num & 0xFFF00000) {
bit = (long long)1 << 60;
} else {
bit = (long long)1 << 36;
}
while (bit > num) bit >>= 2;
for (n = 0; n < 2; n++) {
while (bit) {
if (num >= result + bit) {
num -= result + bit;
result = (result >> 1) + bit;
} else {
result = (result >> 1);
}
bit >>= 2;
}
if (n == 0) {
if (num > 65535) {
num -= result;
num = (num << 16) - 0x8000;
result = (result << 16) + 0x8000;
} else {
num <<= 16;
result <<= 16;
}
bit = 1 << 14;
}
}
if (num > result) {
result++;
}
return (neg ? -result : result);
}
void main() {
float flt_value = 11932767.0f;
long long ll_value = from_float(flt_value);
float flt_root = sqrt(flt_value);
int int_root = (int)fp_sqrt(ll_value);
float flt_root2 = into_float(int_root);
printf("sqrt: %f fp_sqrt: %f", flt_root, flt_root2);
getchar();
}

Related

Why this error happens while showing array out in C?

What I'm making is input a number that type is 'unsigned long long' and then make it to binary form and show it by 16 figures.
Here's my code and result.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
char D_PAN_ID[16];
char D_ADDRESS[16];
char S_PAN_ID[16];
char S_ADDRESS[16];
int main()
{
int bit1, bit2, bit3, bit4, k1, k2, k3, k4;
unsigned long long dec = 5;
/*
printf("8바이트 정수를 이진수로 변환\n");
scanf("%llu", &dec);
*/
printf("%llu 를(을) 이진수로 변환하면:\n", dec);
for (bit1 = 63; bit1 >= 48; bit1--)
{
k1 = dec >> bit1;
if (k1 & 1) {
D_PAN_ID[63 - bit1] = '1';
printf(&D_PAN_ID[63 - bit1]);
}
else {
D_PAN_ID[63 - bit1] = '0';
printf(&D_PAN_ID[63 - bit1]);
}
}
printf("\n\n");
for (bit2 = 47; bit2 >= 32; bit2--)
{
k2 = dec >> bit2;
if (k2 & 1) {
D_ADDRESS[47 - bit2] = '1';
printf(&D_ADDRESS[47 - bit2]);
}
else {
D_ADDRESS[47 - bit2] = '0';
printf(&D_ADDRESS[47 - bit2]);
}
}
printf("\n\n");
for (bit3 = 31; bit3 >= 16; bit3--)
{
k3 = dec >> bit3;
if (k3 & 1) {
S_PAN_ID[31 - bit3] = '1';
printf(&S_PAN_ID[31 - bit3]);
}
else {
S_PAN_ID[31 - bit3] = '0';
printf(&S_PAN_ID[31 - bit3]);
}
}
printf("\n\n");
for (bit4 = 15; bit4 >= 0; bit4--)
{
k4 = dec >> bit4;
if (k4 & 1) {
S_ADDRESS[15 - bit4] = '1';
printf(&S_ADDRESS[15 - bit4]);
}
else {
S_ADDRESS[15 - bit4] = '0';
printf(&S_ADDRESS[15 - bit4]);
}
}
return 0;
}
And what I got is this. I think it is adding upper result at back since printing second result. Like this:
result1
result2 + result1
result3 + result1 + result2
result4 + result1 + result2 + result3
How do I fix it to show like
000000000000
000000000000
000000000000
000000000101

Having trouble passing test case with decimal to binary converter

I have written the code that converts decimal to binary number, but I am having trouble with having a set of inputs, which are 4 test cases and having them to pass.
Can someone help me out with what I am doing wrong?
#include <stdio.h>
int main(void) {
int quotient, i, j, bin[16] = {0};
int decimal1, decimal2, decimal3, decimal4;
decimal1 = 123;
decimal2 = 1024;
decimal3 = 43981;
decimal3 = 2005;
scanf("%d", &quotient);
i = 0;
if (quotient == 0) {
bin[i++] = 0;
}
while (quotient != 0) {
bin[i] = quotient % 2;
quotient = quotient / 2;
i = i + 1;
}
printf(" The Binary value is: ");
for (j = 15; j >= 0; j--)
printf("%d", bin[j]);
return 0;
}
#include <stdio.h>
#include <limits.h>
char *tobin(long value, char *buff, int type)
{
union
{
long l;
unsigned long lu;
}lunion;
char *savedptr = buff;
char *reverse = buff;
char savedchar;
if(type)
{
if(value == LONG_MIN)
{
lunion.lu = 1UL << (sizeof(value) * 8) - 1;
}
else
{
lunion.lu = (value < 0) ? -value : value;
}
}
else
{
lunion.l = value;
}
do
{
*buff++ = (lunion.lu & 1) ? '1' : '0';
lunion.lu >>= 1;
}while(lunion.lu);
if(type && value < 0)
{
*buff++ = '-';
}
*buff-- = 0;
while(buff > reverse)
{
savedchar = *buff;
*buff-- = *reverse;
*reverse++ = savedchar;
}
return savedptr;
}
int main()
{
char x[100];
printf("%ld = %s\n", LONG_MIN, tobin(LONG_MIN, x, 1));
printf("%ld = %s\n", LONG_MIN, tobin(-1, x, 1));
printf("%ld = %s\n", LONG_MIN, tobin(-400, x, 1));
printf("%ld = %s\n", 0L, tobin(0, x, 1));
printf("%ld = %s\n\n\n", LONG_MAX, tobin(LONG_MAX, x, 1));
printf("%ld = %s\n", LONG_MIN, tobin(LONG_MIN, x, 0));
printf("%ld = %s\n", LONG_MIN, tobin(-1, x, 0));
printf("%ld = %s\n", LONG_MIN, tobin(-400, x, 0));
printf("%ld = %s\n", 0L, tobin(0, x, 0));
printf("%ld = %s\n", LONG_MAX, tobin(LONG_MAX, x, 0));
}

Decimal To Binary in C Char Array

I want to return a 16BIT binary from a decimal number. I havt 2 Codes.
In the first one I tried to return a char array.
In the second one a have an output but it in the inverse order.
Maybe someone take a look at the codes.
Thanks
Output first Function: Þ0000000000010000es (x862uÿ¼×
#include <stdio.h>
#include <stdlib.h>
const int BIT = 16;
void binary(int);
char *bin(int);
int main() {
binary(16);
printf("\n");
printf("%s\n", bin(16));
return 0;
}
char *bin(int x) {
char * new = (char*)malloc(sizeof(char) * BIT + 1);
new[BIT] = '\0';
if (x >= 0 && x <= 65535) {
for (int i = 0; i < BIT; i++) {
if (x % 2 == 0) {
new[BIT - i] = '0';
} else {
new[BIT - i] = '1';
}
x = x / 2;
}
}
return new;
}
void binary(int x) {
if (x >= 0 && x <= 65535) {
for (int i = 0; i < BIT; i++) {
if (x % 2 == 0) {
printf("0");
} else {
printf("1");
}
x = x / 2;
}
}
}
I try to answer your question. I am not interested in anybody down-voting me... So I do my very best!!
If you want a binary representation of a decimal value, the easiest thing is to check for bit 0 and then shift the number as long as it is non-zero. Shifting one bit to the right is the same as dividing it by 2.
Instead of checking if the number is between 0 and 65535, it is enough to look if it is zero if you want to abort. If you want a fixed amount of shifts, you can also count from 1 to 16.
So I would suggest something like this:
do {
if (number & 1) {
printf("1");
} else {
printf("0");
}
number >>= 1;
} while(number);
This would output zeros and ones in inversed direction until there are no more ones.
If you want a fixed width representation:
#define BITS 16
for (int bits = 0; bits < BITS; bits++) {
if (number & 1) {
printf("1");
} else {
printf("0");
}
number >>= 1;
}
And if you want to store it into an array, replace printf by
char *bitmap = (char*)malloc(BITS+1);
bitmap[BITS] = '\0'; // NULL termination
bitmap[BITS-1-bits] = '1';
or
bitmap[BITS-1-bits] = '0';
respectively
and have
printf("%s\n", bitmap);
Don't forget to free(bitmap).
Always consider, that an array is zero-based indexed. So it is better to
have bit numbering go from 0 to 15 in case of 16 bits.
I have not tested this code but it works like this. :)
this works, function bin() overwrites array arr in main() ( How do I return a char array from a function? ):
#include <stdio.h>
#include <stdlib.h>
const int BIT = 16;
void binary(int);
void bin(char*, int);
int main(int argc, char* argv[]) {
int x ;
x = atoi(argv[1]);
char arr[BIT+1];
//binary(16);
bin(arr, x); // functions overwrites array arr
arr[BIT] = '\0'; // strings have to be null-terminated in C
printf("%s \n", arr);
return 0;
}
void bin(char *arr1,int x) {
if (x >= 0 && x <= 65535)
{
for (int i = BIT-1; i >= 0; i--)
{
if (x % 2 == 0)
{
arr1[i] = '0';
} else
{
arr1[i] = '1';
}
x = x / 2;
}
}
}

Using arrays for BCD conversion in C

I'm working on a project where I need to convert some long variables into BCD.
I already have some code that works but I feel that it can be improved...
void main(void){
unsigned long input = 0;
unsigned long convert = 0;
float convert2 = 0;
char buffer[200];
unsigned char Ones, Tens, Hundreds, Thousands, TenThousands, HundredThousands;
printf("Input: ");
scanf("%d", &input);
convert = input*12;
convert2 = input * 0.0001224896;
BCD(convert, &Ones, &Tens, &Hundreds, &Thousands, &TenThousands, &HundredThousands);
sprintf(buffer, "%d%d%dKG", HundredThousands, TenThousands, Thousands);
printf("\n\nInputted: %d", input);
printf("\nADC Conversion: %d", convert);
printf("\nBCD Conversion: %s", buffer);
printf("\nFloat Conversion: %f", convert2);
getchar();
getchar();
}
void BCD (unsigned long Pass, unsigned char *Ones, unsigned char *Tens, unsigned char *Hundreds, unsigned char *Thousands, unsigned char *TenThousands, unsigned char *HundredThousands){
unsigned char temp1, temp2, temp3, temp4, temp5, temp6;
unsigned int count = 0;
*Ones = 0;
*Tens = 0;
*Hundreds = 0;
*Thousands = 0;
*TenThousands = 0;
*HundredThousands = 0;
temp1 = 0;
temp2 = 0;
temp3 = 0;
temp4 = 0;
temp5 = 0;
temp6 = 0;
for(count = 0; count <= 31; count++){
if (*Ones >= 5){
*Ones = (*Ones + 3)&0x0F;
}
if (*Tens >= 5){
*Tens = (*Tens + 3)&0x0F;
}
if (*Hundreds >= 5){
*Hundreds = (*Hundreds + 3)&0x0F;
}
if (*Thousands >= 5){
*Thousands = (*Thousands + 3)&0x0F;
}
if (*TenThousands >= 5){
*TenThousands = (*TenThousands + 3)&0x0F;
}
if (*HundredThousands >= 5){
*HundredThousands = (*HundredThousands + 3)&0x0F;
}
temp1 = (Pass & 2147483648) >> 31;
temp2 = (*Ones & 8) >> 3;
temp3 = (*Tens & 8) >> 3;
temp4 = (*Hundreds & 8) >> 3;
temp5 = (*Thousands & 8) >> 3;
temp6 = (*TenThousands & 8) >> 3;
Pass = Pass << 1;
*Ones = ((*Ones << 1) + temp1) & 15;
*Tens = ((*Tens << 1) + temp2) & 15;
*Hundreds = ((*Hundreds << 1) + temp3) & 15;
*Thousands = ((*Thousands << 1) + temp4) & 15;
*TenThousands = ((*TenThousands << 1) + temp5) & 15;
*HundredThousands = ((*HundredThousands << 1) + temp6) & 15;
printf("\n\nLoop: %d\nOnes: %d\n", count, *Ones);
printf("Tens: %d\n", *Tens);
printf("Hundreds: %d\n", *Hundreds);
printf("Thousands: %d\n", *Thousands);
printf("TenThousands: %d\n", *TenThousands);
printf("HundredThousands: %d\n",*HundredThousands);
}
}
The problem I have with this is that it seems messy and inefficient. I was think that instead of using multiple variable for each BCD unit (Ones, Tens, etc), I could use an arrays to carry out the same process. I have implemented this in code but I'm running into a few problems. The code only seems to display "Ones" equivalent element. I've stepped through the code as well and found that the other elements are not being populated during the conversion process. Any guidance on what is going on?
Array implementation:
void main(void){
unsigned long input = 0;
unsigned long convert = 0;
char buffer[200];
unsigned char BCD_Units[6];
unsigned char temp[6];
unsigned int count = 0;
unsigned int count1 = 0;
unsigned char buff_store = 0;
unsigned char buff_store2 = 0;
printf("Input: ");
scanf("%d", &input);
convert = input;
memset(temp, 0, sizeof(temp));
memset(BCD_Units, 0, sizeof(BCD_Units));
for(count = 0; count <= 31; count++){
for (count1 = 0; count1 < 6; count1++){
if (BCD_Units[count1] >= 5){
buff_store = BCD_Units[count1];
buff_store = ((buff_store + 3) & 15);
BCD_Units[count1] = buff_store;
}
}
temp[0] = (convert & 2147483648) >> 31;
for (count1 = 0; count1 < 5; count1++){
buff_store = BCD_Units[count1];
temp[(count+1)] = (buff_store & 8) >> 3;
}
convert = convert << 1;
for(count1 = 0; count1 < 6; count1++){
buff_store = BCD_Units[count1];
buff_store2 = temp[count1];
buff_store = ((buff_store << 1) + buff_store2) & 15;
BCD_Units[count1] = buff_store;
temp[count1] = buff_store2;
}
printf("\n\nLoop: %d\nOnes: %d\n", count, BCD_Units[0]);
printf("Tens: %d\n", BCD_Units[1]);
printf("Hundreds: %d\n", BCD_Units[2]);
printf("Thousands: %d\n", BCD_Units[3]);
printf("TenThousands: %d\n", BCD_Units[4]);
printf("HundredThousands: %d\n", BCD_Units[5]);
}
sprintf(buffer, "%d%d%dKG", BCD_Units[5], BCD_Units[4], BCD_Units[3]);
printf("\n\nInputted: %d", input);
printf("\nBCD Conversion: %s", buffer);
getchar();
getchar();
}
PS. I'm just playing around, at the moment, with ideas. I plan to compartmentalise the code into functions at a later date.
this code seems enormously complicated. YOu just need to do the following
make a buffer
loop till n = 0
get n % 10 (get digit)
or digit into left or right nibble of curretn buffer byte (need a toggle for left or right)
increment buffer pointer if filled left nibble
n = n / 10
try this Convert integer from (pure) binary to BCD

Send a variable to a function and modify this variable in C

I have the following vars:
char seed[NBITS + 1], x0[NBITS + 1], y0[NBITS + 1], z0[NBITS + 1], dT0[NBITS + 1];
And i want to change it values on this function:
void lfsr(char *bin, char *output)
{
//bits significativos para fazer o xor NBITS -> NBITS,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else if(bin[0] == '1')
bits[0] = 1;
if(bin[2] == '0')
bits[1] = 0;
else if(bin[2] == '1')
bits[1] = 1;
if(bin[21] == '0')
bits[2] = 0;
else if(bin[21] == '1')
bits[2] = 1;
if(bin[19] == '0')
bits[3] = 0;
else if(bin[19] == '1')
bits[3] = 1;
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (char)(48 + bit);
output = bin;
}
The way that I put the value in y0 from x is, for example, calling the lfsr functions like this:
lfsr(x0, y0);
What am I doing wrong?
I have to do 3 Fibonacci Linear Feedback Shift Register starting from x0.
x0 = 10101010101010
y0 = lfsr(101010101010)
z0 = lfsr(y0)
dT0 = lfsr(z0);
The results are good, but when I do the above code the value of x0 will be the same as dT0 if i use pointers.
Can anyone help me?
Thanks. Cumps!
Consider the following:
The numbers correspond to the taps. The bits are actually 15..0, left to right. The following is my implementation of the Fibonacci Linear Feedback Shift Register:
#include <stdio.h>
uint16_t fibLfsr(const uint16_t num)
{
uint16_t tempNum;
tempNum = (num) ^ (num >> 2) ^ (num >> 3) ^ (num >> 5);
tempNum = (tempNum & 0x1) << 15;
tempNum = (tempNum | (num >> 1));
return tempNum;
}
int main(void)
{
uint16_t testNum = 0xACE1;
printf("%#X\n", testNum);
testNum = fibLfsr(testNum);
printf("%#X\n", testNum);
return 0;
}
I'm not quite sure why you're using strings and converting them to binary. If this is necessary, you'll need some of the standard library APIs in stdlib and string to convert the string to an uint16_t before calling fibLfsr() and back to a string afterwards.

Resources