Cosa sbaglio?

Forum dedicato ai programmatori di X# in lingua italiana – Italian language forum

Moderator: wriedmann

Post Reply
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Cosa sbaglio?

Post by softdevo@tiscali.it »

LOCAL nDec AS System.Double oppure AS FLOAT oppure AS System.Decimal

nDec := (56/39)

torna sempre 1 e non 1,43.... grazie

Danilo
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

Cosa sbaglio?

Post by Juraj »

maybe my attempt will help

LOCAL nDec as Decimal

nDec:=(57./36.)

return 1.46

Juraj
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Cosa sbaglio?

Post by FFF »

Works, if you write either number with a ".0"
But this is certainly a bug ;)
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3756
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Cosa sbaglio?

Post by wriedmann »

Ciao Danilo,

Juraj is right.

"57" e "39" sono considerati tipo integer, perciò il risultato sarà int, se non imposti /vo12 ("Integer divisions return float").

Altra possibilità:

Code: Select all

nDec := (56d/39d)
nDec := (56m/39m)
Vedi: https://docs.xsharp.it/doku.php?id=literals

Saluti

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

Cosa sbaglio?

Post by Juraj »

just one with a dot (57./39) or (57/39.) return 1.46
User avatar
wriedmann
Posts: 3756
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Cosa sbaglio?

Post by wriedmann »

Hi Karl,

this is not a bug. If the /vo12 compiler option is not selected, every division by integers return an integer.
VO behaves the same.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Cosa sbaglio?

Post by FFF »

Wolfgang,
i see. Hadn't thought that this may be the case even in core ;)

And, as i just found out, it does not. Setting /vo12 makes the compile fail.
For reference tried in c#, the results are the same. So, it's one of these pitfalls, one has to keep in mind....
I think, i'd prefer the opposite behaviour as default, as i can't think of a lot of showcases, where one works on literal ints and wants to get the truncated result. But that's just me ;)
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Cosa sbaglio?

Post by Karl-Heinz »

funny, i firstly thought my cpu is defect, until i´ve noticed that there are different division that cause of course different results :-)

(56/39)
(57./36.)
(57./39)

here´s another float result option

? (decimal) 56/39
? (double) 56/39
? (FLOAT) 56/39

regards
Karl-Heinz
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Cosa sbaglio?

Post by softdevo@tiscali.it »

Thanks to all

Danilo
User avatar
wriedmann
Posts: 3756
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Cosa sbaglio?

Post by wriedmann »

Hi Karl,
And, as i just found out, it does not. Setting /vo12 makes the compile fail.
For reference tried in c#, the results are the same. So, it's one of these pitfalls, one has to keep in mind....
I think, i'd prefer the opposite behaviour as default, as i can't think of a lot of showcases, where one works on literal ints and wants to get the truncated result. But that's just me
so the behaviour of C# is the same as VO and X#.... I would also prefer to have the same as default - had similar problems several times in my VO applications, and therefore I'm always writing numeric literals with decimal point and 0 when dividing, and casting integer variables to float.

But compatibility is important, and changing it would break really a lot of code.

And I can understand why /vo12 makes the compile fail on Core dialect: there is no float datatype.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply