Skip to main content
nickcombs
Inspiring
August 20, 2024
Question

Subtracting UnitValue from Number gives incorrect sign

  • August 20, 2024
  • 10 replies
  • 1265 views

versions tested:

Photoshop 24.7.1

Photoshop 25.5.1

 

platforms tested:

Windows 10

Mac OSX Sonoma

 

steps to reproduce:

Run the following code

var test = 10 - UnitValue( 1, 'px' )
alert( 'expect 9 px:\n'+ test )

 

expected result:

Variable test should return a positive 9 px value

 

actual result:

Variable test returns a negative 9 px value

This topic has been closed for replies.

10 replies

Legend
September 24, 2024

Units are always defined, whether you declare them or not. ALWAYS explicitly declare values in programming. Always. This is CS101 stuff.

nickcombs
nickcombsAuthor
Inspiring
September 24, 2024

@Lumigraphics now you're just hurting your own credibility. Engineering is a pragmatic field and you're arguing theoreticals in a context where it doesn't make sense. You're arguing against making the behavior more consistent.

Legend
September 24, 2024

You are the one with code that doesn't work.

nickcombs
nickcombsAuthor
Inspiring
September 23, 2024

@Lumigraphics 

 

I see where you're coming from, but there are reasons to expect this to work.

 

These establish an expectation that the unitless value will be assigned the unit during the arithmetic operation:

1 + UnitValue( 9, 'px' ) => 10 px

UnitValue( 9, 'px' ) + 1 => 10 px

UnitValue( 9, 'px' ) - 1 => 8 px

 

Since UnitValue is not supported in Math methods, there's a need to convert back and forth to unitless values. External data sources also don't have UnitValue built in, so we run into the same messiness there. We also get unitless values from certain properties in the Illustrator DOM (and maybe more) where we'd expect UnitValue types. I know this is in the Ps forum, but the issue persists across multiple apps.

 

If a single conversion back to UnitValue is missed, the script can give the wrong result without any exception to catch and the math operation itself will look correct.

Stephen Marsh
Community Expert
Community Expert
September 20, 2024

@Lumigraphics – now that you point that out it of course makes sense!

 

var test = UnitValue( 10, 'px' ) - UnitValue( 1, 'px' )
alert( 'expect 9 px:\n'+ test )
Legend
September 20, 2024

Oh and conversions should be to fixed values. If I get a mix of units, again... 5 miles, 12 feet, 16 parsecs, those all have fixed conversions.

Legend
September 20, 2024

Your equation is meaningless, its like saying I'm five away. Minutes? Miles? Light-years?

Explicitly set your units so you know what you are working with.

If your units are points, 1 point minus 9 pixels equals... ?

For yards: 1 minus 9 miles equals... ?

This is a logic error. You didn't declare units so you won't get reliable results.

nickcombs
nickcombsAuthor
Inspiring
September 20, 2024

Just checking in on the progress of this ticket. It's been a month with no response.

 

This might be another good way to test it:

-UnitValue( 1, 'px' ) + 10

 

Stephen Marsh
Community Expert
Community Expert
August 21, 2024
quote

Yeah I agree there are workarounds.


By @nickcombs

 

 

It's an "interesting" bug!  :]

 

The following is for my future reference, if I remember!

 

var test = Math.abs(10 - UnitValue( 1, 'px' ));
alert( 'expect 9 px:\n'+ test );

 

var test = -(10 - UnitValue( 1, 'px' ));
alert( 'expect 9 px:\n'+ test );

 

var test = 10 - UnitValue( 1, 'px' );
test = test * -1;
alert( 'expect 9 px:\n'+ test );

 

nickcombs
nickcombsAuthor
Inspiring
August 20, 2024

Yeah I agree there are workarounds. Unfortunately there's no simple fix in my case. I'm currently encountering it in a process that dynamically sizes elements to existing dimensions read from the document, so I'm receiving UnitValues and trying to convert everything to Numbers so that I can pass them into Math methods. I'm also working with JSON data that contains lengths stored in various units, and those need to be converted with UnitValue.as(). 

Stephen Marsh
Community Expert
Community Expert
August 20, 2024

I have always had mixed feelings about using UnitValue and have often resorted to the following:

 

var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var test = 10 - 1;
alert( 'expect 9 px:\n'+ test );
app.preferences.rulerUnits = origRulerUnits;

 

With "the rise of UXP", I would hope that Adobe will still fix bugs in ExtendScript until it is no longer supported. 

nickcombs
nickcombsAuthor
Inspiring
August 20, 2024

I have also confirmed this bug persists across multiple apps:

Illustrator 27.9 (64-bit)

InDesign 19.3 x64