It looks like you're new here. If you want to get involved, click one of these buttons!

- 7.5K All Categories
- 10 Help with translations
- 4.1K General questions
- 101 Roadmap
- 344 Game & application design
- 299 Plugins
- 68 User experience
- 74 Marketplace
- 284 Code snippets
- 33 Building a team?
- 261 Suggestions & requests
- 347 Announce your apps made with Gideros.
- 90 Step by step tutorials
- 621 Bugs and issues
- 194 Introduce yourself
- 204 Announcements
- 99 Forum talk
- 395 Relax cafe

oleg
Member

Is there a better function to get a number sign?

a=-456 anti_abs=math.abs(a)/a print(anti_abs) --> -1 a=456 anti_abs=math.abs(a)/a print(anti_abs) --> 1 |

my games:

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

## Comments

https://stackoverflow.com/questions/441893/which-is-faster-math-absvalue-or-value-1

my growING GIDEROS github repositories:https://github.com/mokalux?tab=repositoriesLikes: MoKaLux

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

Likes: oleg

my growING GIDEROS github repositories:https://github.com/mokalux?tab=repositoriesLikes: oleg

Fragmenter - animated loop machine and IKONOMIKON - the memory game

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

Likes: oleg

https://deluxepixel.com

All the examples are wrong with zero ...

Exept this..

if a<0 then return -1 else return 1 end

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

Core i7-8700: Android Snapdragon 660:

I guess, #2 or #3 is ok. Less than a sec., shoud be good

EDIT:Found one more way:

EDIT2Hmm, I wonder why bit operations slower that just

?"x < y and a or b"This give us -1 for neagative and 1 for 0 and positive numbers

Likes: MoKaLux, hgy29, oleg

Zero itself is without a sign, or signless.

Likes: SinisterSoft

Fragmenter - animated loop machine and IKONOMIKON - the memory game

All other values except "-1" and "+1" will cause errors in the program

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

Fragmenter - animated loop machine and IKONOMIKON - the memory game

For my program only 3 ways will not cause an error in the program:

1.@rrraptor

a<0 and -1 or a > 0 and 1 or 0

2.@SinisterSoft

a=0

anti_abs=(a<>-1)><1

print(anti_abs)

3.if a<0 then return -1 else return 1 end

And my example and example @hgy29 will give a value of 'nan' and cause an error

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

So there are only '-1' and '+1' and '0' is not allowed

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

test7 results with:

-1 if neg, 0 if 0, +1 if pos

test8 result with:

-1 if neg, 0 if not neg

function test7()

for loop=1,max do

local sign =(a<>-1)><1

end

end

function test8()

for loop=1,max do

local sign =(a<>-1)><0

end

end

test1 4.5261051000002

test2 0.23030710000012

test3 0.25647970000023

test4 4.9691175000003

test5 0.48598210000046

test6 0.3186288000004

test7 0.17589819999989

test8 0.17575890000012

Likes: oleg

https://deluxepixel.com

I also added a test9 in case being -1 or +1 was important, test9 will give -1 if neg and +1 if pos (inc zero)...

function test9()

for loop=1,max do

local sign =((a<>-1)><0)|1

end

end

test2 0.23013360000004

test3 0.25745690000076

test5 0.37778469999967

test6 0.31808180000007

test7 0.17587430000003

test8 0.17580960000032

test9 0.24851379999927

I removed the two crazy slow functions (1 and 4)!

I think the bitwise operators deal with real numbers so they have to do conversions, so they slow down. If they were true bitwise operators then I think 9 would be faster than 2.

Likes: oleg

https://deluxepixel.com

test7

sign=a<>-1><1

test8

sign=a<>-1><0

test9

sign=a<>-1><0|1

timings are the same though, just saves typing!

Likes: oleg

https://deluxepixel.com

-3,, -2, -1, 0, 1 ,2

Likes: antix

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

https://deluxepixel.com

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

Assuming '-0' is an infinite negative number '-0,000000000000001'

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

For example, there is a math.atan2 function which correctly returns zero

Instead of the math.atan function

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!

ps/ The sign -0 cannot be because:

-0 = x*-0%

-1 = x*-100%

The sign number is always the digit '1'

33*-1=-33

33*-0 =0 error!!

Likes: antix

https://play.google.com/store/apps/developer?id=razorback456

мій блог по гідерос https://simartinfo.blogspot.com

Слава Україні!