Decimal to IEEE 754 Single-precision IEEE 754 code using C
We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.
This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.
The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “
==============================================================================
The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.
I would really appreciate it if someone can guide me through making this code!
Thanks in advanced :)
c binary ieee-754 converters single-precision
add a comment |
We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.
This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.
The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “
==============================================================================
The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.
I would really appreciate it if someone can guide me through making this code!
Thanks in advanced :)
c binary ieee-754 converters single-precision
2
"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.
– user694733
Nov 15 '18 at 9:28
add a comment |
We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.
This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.
The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “
==============================================================================
The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.
I would really appreciate it if someone can guide me through making this code!
Thanks in advanced :)
c binary ieee-754 converters single-precision
We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.
This is the assignment:
The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.
The ouput of the program must be equal to the following :
“Introduce a float point number in decimal (scientific notation):
The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754). “
==============================================================================
The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned "struct" or "union" or any of that yet, because I saw those in other examples and I didn't understand anything.
I would really appreciate it if someone can guide me through making this code!
Thanks in advanced :)
c binary ieee-754 converters single-precision
c binary ieee-754 converters single-precision
asked Nov 15 '18 at 9:12
Nor AlexanianNor Alexanian
13
13
2
"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.
– user694733
Nov 15 '18 at 9:28
add a comment |
2
"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.
– user694733
Nov 15 '18 at 9:28
2
2
"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.
– user694733
Nov 15 '18 at 9:28
"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.
– user694733
Nov 15 '18 at 9:28
add a comment |
2 Answers
2
active
oldest
votes
guide me through making this code!
As OP only requested a guide:
- gets a scientific notation floating point number
Research scanf()
and the "%f"
specifier
- prints it´s IEE754 single precision value in ... hexadecimal
Research type punning a float
into uint32_t
. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?
Research printf()
and the "%x"
specifier.
- prints ... in ... binary
There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
The assignment you describe has three parts:
Examine the characters of the numeral and convert them into a floating-point number.
Print the value of the floating-point number.
Print the bytes of a floating-point number.
For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf
(to read from input) or sscanf
(to read from an array of char
). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int
, then multiply 1 by pow(10, -5)
, multiply 2 by pow(10, -6)
, and so on, and then add the products.
For 2, print using printf
.
For 3, use memcpy
to copy the bytes from the float
into a uint32_t
(look up the header <stdint.h>
) and print its bytes with printf
(look up the header <inttypes.h>
for the correct format specifier to use). Alternatively, use memcpy
to copy the bytes from the float
into an array of char
, then print the char
using printf
with a format specifier that prints hexadecimal.
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315908%2fdecimal-to-ieee-754-single-precision-ieee-754-code-using-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
guide me through making this code!
As OP only requested a guide:
- gets a scientific notation floating point number
Research scanf()
and the "%f"
specifier
- prints it´s IEE754 single precision value in ... hexadecimal
Research type punning a float
into uint32_t
. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?
Research printf()
and the "%x"
specifier.
- prints ... in ... binary
There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
guide me through making this code!
As OP only requested a guide:
- gets a scientific notation floating point number
Research scanf()
and the "%f"
specifier
- prints it´s IEE754 single precision value in ... hexadecimal
Research type punning a float
into uint32_t
. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?
Research printf()
and the "%x"
specifier.
- prints ... in ... binary
There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
guide me through making this code!
As OP only requested a guide:
- gets a scientific notation floating point number
Research scanf()
and the "%f"
specifier
- prints it´s IEE754 single precision value in ... hexadecimal
Research type punning a float
into uint32_t
. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?
Research printf()
and the "%x"
specifier.
- prints ... in ... binary
There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)
guide me through making this code!
As OP only requested a guide:
- gets a scientific notation floating point number
Research scanf()
and the "%f"
specifier
- prints it´s IEE754 single precision value in ... hexadecimal
Research type punning a float
into uint32_t
. Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?
Research printf()
and the "%x"
specifier.
- prints ... in ... binary
There a many ways to Is there a printf converter to print in binary format?.
Some are very general (base, 2,3,10,16,36...)
edited Nov 15 '18 at 17:52
answered Nov 15 '18 at 13:56
chuxchux
84k872154
84k872154
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
I will take a look at the links and try them out, ill let you know if it works
– Nor Alexanian
Nov 15 '18 at 17:41
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
Got the code to work, thank youu
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
The assignment you describe has three parts:
Examine the characters of the numeral and convert them into a floating-point number.
Print the value of the floating-point number.
Print the bytes of a floating-point number.
For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf
(to read from input) or sscanf
(to read from an array of char
). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int
, then multiply 1 by pow(10, -5)
, multiply 2 by pow(10, -6)
, and so on, and then add the products.
For 2, print using printf
.
For 3, use memcpy
to copy the bytes from the float
into a uint32_t
(look up the header <stdint.h>
) and print its bytes with printf
(look up the header <inttypes.h>
for the correct format specifier to use). Alternatively, use memcpy
to copy the bytes from the float
into an array of char
, then print the char
using printf
with a format specifier that prints hexadecimal.
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
The assignment you describe has three parts:
Examine the characters of the numeral and convert them into a floating-point number.
Print the value of the floating-point number.
Print the bytes of a floating-point number.
For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf
(to read from input) or sscanf
(to read from an array of char
). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int
, then multiply 1 by pow(10, -5)
, multiply 2 by pow(10, -6)
, and so on, and then add the products.
For 2, print using printf
.
For 3, use memcpy
to copy the bytes from the float
into a uint32_t
(look up the header <stdint.h>
) and print its bytes with printf
(look up the header <inttypes.h>
for the correct format specifier to use). Alternatively, use memcpy
to copy the bytes from the float
into an array of char
, then print the char
using printf
with a format specifier that prints hexadecimal.
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
The assignment you describe has three parts:
Examine the characters of the numeral and convert them into a floating-point number.
Print the value of the floating-point number.
Print the bytes of a floating-point number.
For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf
(to read from input) or sscanf
(to read from an array of char
). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int
, then multiply 1 by pow(10, -5)
, multiply 2 by pow(10, -6)
, and so on, and then add the products.
For 2, print using printf
.
For 3, use memcpy
to copy the bytes from the float
into a uint32_t
(look up the header <stdint.h>
) and print its bytes with printf
(look up the header <inttypes.h>
for the correct format specifier to use). Alternatively, use memcpy
to copy the bytes from the float
into an array of char
, then print the char
using printf
with a format specifier that prints hexadecimal.
The assignment you describe has three parts:
Examine the characters of the numeral and convert them into a floating-point number.
Print the value of the floating-point number.
Print the bytes of a floating-point number.
For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf
(to read from input) or sscanf
(to read from an array of char
). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int
, then multiply 1 by pow(10, -5)
, multiply 2 by pow(10, -6)
, and so on, and then add the products.
For 2, print using printf
.
For 3, use memcpy
to copy the bytes from the float
into a uint32_t
(look up the header <stdint.h>
) and print its bytes with printf
(look up the header <inttypes.h>
for the correct format specifier to use). Alternatively, use memcpy
to copy the bytes from the float
into an array of char
, then print the char
using printf
with a format specifier that prints hexadecimal.
answered Nov 15 '18 at 14:00
Eric PostpischilEric Postpischil
78.5k883166
78.5k883166
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Thanks for the reply! I will try out the code and let you know how it turns out
– Nor Alexanian
Nov 15 '18 at 17:40
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
Just letting u know, i got it to work :)
– Nor Alexanian
Nov 21 '18 at 22:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315908%2fdecimal-to-ieee-754-single-precision-ieee-754-code-using-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
"I would really appreciate it if someone can guide me through making this code!" That is not how this site works. You need to ask specific question, which is answerable with specific answer. Break your problem to smaller problems, then try to solve one smaller problem. If you have issue with that small problem, then ask about that. See How to Ask.
– user694733
Nov 15 '18 at 9:28