It is currently Tue Nov 12, 2019 10:45 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Strange behavior with Round(), Val() and .l variable type
PostPosted: Wed Feb 27, 2013 4:51 pm 
Offline
Addict
Addict

Joined: Thu Nov 01, 2007 5:37 pm
Posts: 1981
Location: Germany
I'm not sure what exactly the cause of this problem is... Round(), Val(), 1*1.0, v=v+1, type of variable, combination of all that or something else?
Anyway, there is definitely something going wrong which leads to unexpected behavior. Tested with PB 4.61, 5.00, 5.10 on Windows XP x86.

Demo code:
Code:
Define.l Test0, Test1, Test2, Test3, Test4  ; Error with .l and .w (.b, .a and .u too?!)


; Works as expected:
Debug Round(Val("1") * 1.0, #PB_Round_Nearest)  ; Returns "1.0"

Test0 = Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test0  ; Returns "1"


; Strange things going on:
Test1 = 1
Test1 = Test1 + Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test1  ; Returns "-2147483648", "2" expected

Test2 = 1
Test2 = Test2 - Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test2  ; Returns "-2147483648", "0" expected

Test3 = 1
Test3 = Test3 * Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test3  ; Returns "-2147483648", "1" expected

Test4 = 1
Test4 = Test4 / Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test4  ; Returns "-2147483648", "1" expected

_________________
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 5:11 pm 
Offline
666
666

Joined: Mon Sep 01, 2003 2:33 pm
Posts: 1033
...


Last edited by LuCiFeR[SD] on Wed Feb 27, 2013 5:15 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 5:12 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5845
Location: Wales, UK
Code:
Test1.d = 1
Test1 = (Test1 + Round(Val("1") * 1.0, #PB_Round_Nearest))
Debug Test1  ; Returns "-2147483648", "2" expected

Test2.d = 1
Test2 = Test2 - Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test2  ; Returns "-2147483648", "0" expected

Test3.d = 1
Test3 = Test3 * Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test3  ; Returns "-2147483648", "1" expected

Test4.d = 1
Test4 = Test4 / Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test4  ; Returns "-2147483648", "1" expected

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 5:17 pm 
Offline
Addict
Addict

Joined: Thu Nov 01, 2007 5:37 pm
Posts: 1981
Location: Germany
You think returning "-2147483648" in that case is normal?

Also why does this work?
Code:
Test1 = 1 + Round(Val("1") * 1.0, #PB_Round_Nearest)
Debug Test1
...or this:
Code:
Test2 = 1
Test2 = Test2 + Round(1 * 1.0, #PB_Round_Nearest)

...or this:
Code:
Test = 1
Test = Test + (Val("1") * 1.0)


@IdeasVacuum
I'm aware of that, that's why I wrote "Error with .l and .w (.b, .a and .u too?!)". ;)


Edit: Added more example codes.

_________________
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!


Last edited by c4s on Wed Feb 27, 2013 5:25 pm, edited 4 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 5:21 pm 
Offline
666
666

Joined: Mon Sep 01, 2003 2:33 pm
Posts: 1033
Quote:
Syntax

Result.f = Round(Number.f, Mode)
Description

Round the specified float number according to the given mode.


@c4s Round returns a float. ValF is what you should be using.


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 5:53 pm 
Offline
Addict
Addict

Joined: Thu Nov 01, 2007 5:37 pm
Posts: 1981
Location: Germany
LuCiFeR[SD] wrote:
@c4s Round returns a float. ValF is what you should be using.
Yeah, but shouldn't this be automatically transformed so that it works correctly? Also what about the other examples?

_________________
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 5:54 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3699
Location: Berlin, Germany
Same bug with type .i
(PB 5.10 final 32 bit on Windows XP SP3 32 bit)

Code:
EnableExplicit
Define.i Test0

; OK when using 1 as literal constant:
Debug 1 + Round(Val("1") * 1.0, #PB_Round_Nearest)      ; -> 2.0

; Error when using 1 as integer variable:
Test0 = 1
Debug Test0 + Round(Val("1") * 1.0, #PB_Round_Nearest)  ; -> NaN

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 9:57 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1685
Location: Uttoxeter, UK
Hi Guys:

I tried c4s's 1st post and got all the expected results.

I also tried Little John's and got 2.0 both times.

So no problem here.

Using PB5.10 with windows 7/64

_________________
DE AA EB


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Wed Feb 27, 2013 10:40 pm 
Offline
User
User

Joined: Fri Nov 30, 2012 7:21 pm
Posts: 16
Little John wrote:
Same bug with type .i
(PB 5.10 final 32 bit on Windows XP SP3 32 bit)

Code:
EnableExplicit
Define.i Test0

; OK when using 1 as literal constant:
Debug 1 + Round(Val("1") * 1.0, #PB_Round_Nearest)      ; -> 2.0

; Error when using 1 as integer variable:
Test0 = 1
Debug Test0 + Round(Val("1") * 1.0, #PB_Round_Nearest)  ; -> NaN


I got the same results on 5.10 final 32 bit Linux.


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Thu Feb 28, 2013 2:13 am 
Offline
Addict
Addict

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2736
Location: Southwest OH - USA
I get the same results as Little John, 2 and NaN, on windows 8 64 bit using PB 5.1 32 bit.

cheers


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Thu Feb 28, 2013 6:26 am 
Offline
User
User

Joined: Mon Apr 23, 2012 1:39 pm
Posts: 45
Little John wrote:
Same bug with type .i
(PB 5.10 final 32 bit on Windows XP SP3 32 bit)

Code:
EnableExplicit
Define.i Test0

; OK when using 1 as literal constant:
Debug 1 + Round(Val("1") * 1.0, #PB_Round_Nearest)      ; -> 2.0

; Error when using 1 as integer variable:
Test0 = 1
Debug Test0 + Round(Val("1") * 1.0, #PB_Round_Nearest)  ; -> NaN


Confirmed - Windows 7 SP1 x64 - PureBasic 5.10 (x86)


Top
 Profile  
Reply with quote  
 Post subject: Re: Strange behavior with Round(), Val() and .l variable typ
PostPosted: Thu Feb 28, 2013 6:24 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3699
Location: Berlin, Germany
LuCiFeR[SD] wrote:
Quote:
Syntax

Result.f = Round(Number.f, Mode)
Description

Round the specified float number according to the given mode.


@c4s Round returns a float. ValF is what you should be using.

Hi,

I've read your message before, but the penny didn't drop. :oops:
Now I think that you are right.

I just tested it: After replacing Val with ValF or ValD, all of c4s' examples as well as my example work correctly.
I think the reason why I (and maybe others) were confused probably was, that using Val sometimes yields a correct result. But since Val shouldn't have been used at all in all these examples, it doesn't matter whether or not "illegal" use of Val sometimes yields correct results by chance. When we use ValF or ValD in those examples (according to the documentation), we'll always get correct results.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye