Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

reverse a float number in C language by basic commands.

Status
Not open for further replies.

ghasem_008

Full Member level 4
Joined
Feb 9, 2012
Messages
221
Helped
11
Reputation
22
Reaction score
10
Trophy points
1,298
Activity points
2,990
hi.
is there anyone who can write a full program that reverse a float number in C language?
I want to use from following commands:
1. if/for/while loops
2. I dont want use from any Function.please write a code with basic commands.

I wrote a code,but it's not true for some numbers.
for example : 12.001

//my code:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
 
int main()
{
 
 
    double a;
    char ans_exit='y';
    while(ans_exit='y')
    {
    printf("enter a number:");
    scanf("%lf",&a);
 
    // integer and decimal sections:
    int a1 = (int)a;
    float a2 = a-a1;
    printf("\n\nfull number is %f\n",a);
    printf("integer section of %f is:%d\n",a,a1);
    printf("decimal section of %f is:%f\n\n",a,a2);
 
 
    int i=0,j=1;
    int b;
    do
    {
        b = a1%10;
        a1/=10;
        if (j==1)
        {
            // decimal to integer!
            int a2_1;
            float a2_2;
            int ax;
            while ((a2-floor(a2))!=0)
            {
                a2*=10;
                //printf("\n\na2=%f , int(a2)=%d\n\n",a2,ax);
            }
            int a3,c2;
            a3 = round(a2);
            printf("\n\na2 = %f , a3=%d\n\n",a2,a3);
             do
            {
                c2 = a3%10;
                a3/=10;
                printf("%d",c2);
            }while(a3!=0);
            printf(".");
        }
        printf("%d",b);
        j++;
 
    }while(a1!=0);
 
    printf("\n\nDo you want to repeat program?(yes=y,no=n):");
    ans_exit = getche();
    printf("\n\n");
    }
 
    return 0;
}


View attachment my_code.txt
 
Last edited by a moderator:

BradtheRad

Super Moderator
Staff member
Joined
Apr 1, 2011
Messages
13,792
Helped
2,735
Reputation
5,466
Reaction score
2,645
Trophy points
1,393
Location
Minneapolis, Minnesota, USA
Activity points
102,946
I don't read C, but just to give some kind of reply...

reverse a float number
Which do you wish to do:

* change the sign?
* calculate a reciprocal?
* change floating point to fixed? To integer?

- - - Updated - - -

* change the sequence of MSB and LSB?
 

ghasem_008

Full Member level 4
Joined
Feb 9, 2012
Messages
221
Helped
11
Reputation
22
Reaction score
10
Trophy points
1,298
Activity points
2,990
I want to reverse a float number that is entered by user in the input.
float number = integer section . decimal section
for example if you entered 13.546 in the input, you have to receive 645.31 in the output.
my code work well for many float numbers,but in some cases (such as when decimal section include several zeros) dont work and decimal section doesnt reverse truly.
how remove extra digits in decimal section in a float number that appear in C language?
for example when you enter 12.001 in the input,C language receive 12.0010001.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,567
Helped
4,776
Reputation
9,569
Reaction score
4,557
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
124,188
Due to the way a floating point number is stored, it is difficult to rearrange the bits to reverse it. I would suggest thge best method is to convert it to a character string, reverse that and then convert it back to a float.

untested code:
Code:
sprintf(FloatString,"%f",FloatValue);
strrev(FloatString);
sscanf(FloatString,"%f",&ReversedFloatValue);
No compiler in my mobile phone so please check your compiler documentation for exact syntax!

Brian.
 

CataM

Advanced Member level 4
Joined
Dec 23, 2015
Messages
1,275
Helped
314
Reputation
628
Reaction score
312
Trophy points
83
Location
Madrid, Spain
Activity points
8,416
It has been a while since I last used C language. I have tried to do it but it is hard because of the zeros in the decimal part. I am using Visual Studio 2010 and in that, the number 12.001 is stored as 12.001000 (they add number of zeros to have 6 digits in the decimal part).


I think your problem could be solved by solving another problem first which is to find out how many digits (integer and decimal) have the number introduced by the user and then is easy to solve.

Other solution might be by cheating. Let me explain what I mean with "cheating".
I mean that you know how your program work when scan a float number. I will explain it with an example using my programme and then try to apply this to your:

So my programme adds the necesarry number of zeros to have 6 digits on the decimal part.
If I introduce 12.1, they store it like 12.100000. If I introduce 12.12345 is stored 12.123450. If I introduce 12.123456 is stored like 12.123456 (no more zeros... this is the clue).
So the cheating means understand how the program stores the float number.

Now that you know that, because you don't know how many digits the user will introduce, just compare the decimal part of the number introduced by the user and some decimal parts...

This could be done by multiplying the decimal part by 10^(6) and get every digit of it and start comparing one by one.
Yes... is not the "engineering solution" but it might work... With float numbers is difficult because in real life a float number have infinite zeros on the decimal part.. so you must know how your program store the number.
 
Last edited:

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
47,841
Helped
14,116
Reputation
28,489
Reaction score
12,797
Trophy points
1,393
Location
Bochum, Germany
Activity points
277,694
Code:
sprintf(FloatString,"%f",FloatValue);
strrev(FloatString);
sscanf(FloatString,"%f",&ReversedFloatValue);
The OP doesn't attempt to put the "reversed float" into one variable, so your code does already more than the original code.

strrev() isn't present in all string libraries, could be replaced by a loop construct.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top