|
|
|
|
Forum
|
|
|
A little bit help with tinycc compiler, the problem is that for a reason I can't pass FLOATS to function, which is inside the main problem
|
|
| darkmoonmaster |
|
New Member

Group: Members
Posts: 1
Member No.: 40412
Joined: 15-December 10

|
I am using the tinycc mini compiler for generating dynamic code for a small project. The problem is that for a reason i can't pass FLOATS to function, which is inside the main problem. But if I change it to double it works. This is my code.
#include <iostream>
#include "libtcc.h"
using namespace std;
void local( float f ) { printf ( "%f \n", f ); }
void main() { TCCState *s; void (*main)(); void *mem; int size=0;
// This is our code char code[] = " void main () { local( 3.1416); }" ;
s = tcc_new();
if (!s) cout << "Error" << endl;
/* MUST BE CALLED before any compilation */ tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
if ( tcc_compile_string(s, code) == -1) cout << "Error" << endl;
tcc_add_symbol(s, "printf", printf); tcc_add_symbol(s, "local", local);
size = tcc_relocate(s, NULL); if (size == -1) cout << "Error" << endl;
mem = malloc(size); tcc_relocate(s, mem);
main =( void (*) (void) ) tcc_get_symbol(s, "main");
if (!main) cout << "Error" << endl;
// call the main function main();
system ( "pause" ); }
|
| |
|
|
|
| MarMo |
|
New Member

Group: Members
Posts: 19
Member No.: 39492
Joined: 14-November 10

|
try to explicitly indicate that the past is a float value because you are not initiated this value in any variable type float, switching to (float f) the value 3.1416 to the first decision of compiador will identify this parameter as type double. try this: char code [] = "void main () {local (3.1416f);} ";
if the error occurs again tell me to try to find another way to show the compiler that this is a float. My doubt is that this compiler using does not know him to be honest but I know there are rules and should be followed. I'm basing that.
Here's a short explanation on working with floats, they are always complicated and must be well defined for the compiler does not go into alert condition or errors. There are cases where it is not generated any error or warning but the results are submitted with errors or differences.
The binary representation of the decimal number may not be exact.
There is a type mismatch between the numbers used (for example, mixing float and double). To resolve the behavior, most programmers either ensure that the value is greater or less than what is needed, or they get and use a Binary Coded Decimal (BCD) library that will maintain the precision.
Sample /* Compile options needed: none. Value of c is printed with a decimal point precision of 10 and 6 (printf rounded value by default) to show the difference */ #include <stdio.h> #define EPSILON 0.0001 // Define your own tolerance #define FLOAT_EQ(x,v) (((v - EPSILON) < x) && (x <( v + EPSILON))) void main() { float a,b,c a=1.345f; <-- telling the compiler that the variable is receiving a float value b=1.123f; <-- no indication that the compiler generates a warning or error in conversion (depending on the compiler and standards used) c=a+b; //if (FLOAT_EQ(c, 2.468)) // Remove comment for correct result if (c == 2.468) //Comment this line for correct result printf("They are equal\n"); else printf("They are not equal!!The value of c is %13.10f,or %f",c,c); } The Output Result They are not equal. The value of c is 2.4679999352 or 2.468000.
|
| |
|
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Track this topic
Receive email notification when a reply has been made to this topic and you are not active on the board.
Subscribe to this forum
Receive email notification when a new topic is posted in this forum and you are not active on the board.
Download / Print this Topic
Download this topic in different formats or view a printer friendly version.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|