AdeptLanguage / Adept

The Adept Programming Language
GNU General Public License v3.0
119 stars 9 forks source link

No value in Float_printf ::::: Adept 2.4 #60

Closed Spoiledpay closed 2 years ago

Spoiledpay commented 2 years ago

I'm getting to know the language better and testing some code. And note that the code below in the "Float" number does not show. Getting zero. (0). And it should appear in Number1 = 13.5

main.adept (file)

import "sys/cstdio.adept" //Use' ' func main(in argc int, in argv **ubyte) int {

 number1 float = 13.5f;
 number2 double = 12.4;

printf('number1 = %lf\n', number1);
printf('number2 = %lf', number2);
return 0;

}

output:

C:\Adept\Example\printf>p3.exe number1 = 0.000000 number2 = 12.400000

IsaacShelton commented 2 years ago

@Spoiledpay

This is a side-effect of how printf works in C.

When you call printf in C, the compiler will make sure that any float values you pass it are casted to double (which doesn't happen for other vararg functions)

Adept won't convert your float to a double automatically when you pass it as a variadic argument. The reason why it prints 0.000000 is because the C version of printf expects a double for a variadic argument and not float.

import basics

func main {
    number1 float = 13.5f
    number2 double = 12.4

    // Using the C version of printf  -  printf(*ubyte, ...) int
    // '%f' expects only `double`

    printf('number1           = %f\n', number1)
    printf('number1 as double = %f\n', number1 as double)
    printf('number2           = %f\n', number2)
    printf('-----------------------------\n')

    // Using the Adept version of printf  -  printf(format String, arguments ...) int
    // '%f' expects `double` or `float`

    printf("number1           = %f\n", number1)
    printf("number2           = %f\n", number2)
}
number1           = 0.000000
number1 as double = 13.500000
number2           = 12.400000
-----------------------------
number1           = 13.5
number2           = 12.4

The difference is that the Adept version of printf takes a String instead of a *ubyte as its format. Strings use double quotes, and C-Strings use single quotes.

printf('C-String')
printf("Regular String")

More information about Strings can be found here: In-depth information about String

You can also look at the implementation of Adept printf(String, args ...) int here: Adept printf implementation

The Adept version printf(String, args ...) int of printf comes from 2.5/cstdio.adept via 2.5/basics.adept. You see more here: 2.5/cstdio.adept documentation