1- import { normalizeAngle , degreesToDMS } from '../../../src/compute-engine/latex-syntax/serialize-dms' ;
1+ import {
2+ normalizeAngle ,
3+ degreesToDMS ,
4+ } from '../../../src/compute-engine/latex-syntax/serialize-dms' ;
25import { ComputeEngine } from '../../../src/compute-engine' ;
36
47describe ( 'Degrees Function Serialization' , ( ) => {
@@ -79,6 +82,15 @@ describe('Angle Normalization', () => {
7982 expect ( normalizeAngle ( - 190 , '-180...180' ) ) . toBeCloseTo ( 170 , 10 ) ;
8083 expect ( normalizeAngle ( 370 , '-180...180' ) ) . toBeCloseTo ( 10 , 10 ) ;
8184 } ) ;
85+
86+ test ( 'avoids negative zero' , ( ) => {
87+ expect ( Object . is ( normalizeAngle ( - 360 , '0...360' ) , - 0 ) ) . toBe ( false ) ;
88+ expect ( normalizeAngle ( - 360 , '0...360' ) ) . toBe ( 0 ) ;
89+ expect ( Object . is ( normalizeAngle ( 0 , '-180...180' ) , - 0 ) ) . toBe ( false ) ;
90+ expect ( normalizeAngle ( 0 , '-180...180' ) ) . toBe ( 0 ) ;
91+ expect ( Object . is ( normalizeAngle ( - 360 , '-180...180' ) , - 0 ) ) . toBe ( false ) ;
92+ expect ( normalizeAngle ( - 360 , '-180...180' ) ) . toBe ( 0 ) ;
93+ } ) ;
8294} ) ;
8395
8496describe ( 'DMS Formatting' , ( ) => {
@@ -108,6 +120,13 @@ describe('DMS Formatting', () => {
108120 const result = degreesToDMS ( 9.504166666666 ) ;
109121 expect ( result . sec ) . toBeCloseTo ( 15 , 2 ) ;
110122 } ) ;
123+
124+ test ( 'avoids negative zero in components' , ( ) => {
125+ // -1.0 degrees: deg=-1, min=0, sec=0 (not -0)
126+ const result = degreesToDMS ( - 1.0 ) ;
127+ expect ( Object . is ( result . min , - 0 ) ) . toBe ( false ) ;
128+ expect ( result . min ) . toBe ( 0 ) ;
129+ } ) ;
111130} ) ;
112131
113132describe ( 'Round-Trip Parsing and Serialization' , ( ) => {
@@ -117,39 +136,34 @@ describe('Round-Trip Parsing and Serialization', () => {
117136 const input = "9°30'15\"" ;
118137 const expr = ce . parse ( input ) ;
119138
120- // Evaluate to get a single Quantity , then serialize with DMS format
139+ // Evaluate to get a numeric value , then serialize with DMS format
121140 const evaluated = expr . N ( ) ;
122141 const serialized = evaluated . toLatex ( { dmsFormat : true } ) ;
123142
124- // Should serialize to same or equivalent DMS
143+ // Reparse and re-evaluate
125144 const reparsed = ce . parse ( serialized ) ;
126145 const reparsedEvaluated = reparsed . N ( ) ;
127146
128147 // Compare the numeric values (should be approximately equal)
129- expect ( evaluated . json ) . toEqual ( [ 'Quantity' , { num : '9.504166666666666' } , 'deg' ] ) ;
130- expect ( reparsedEvaluated . json ) . toEqual ( [ 'Quantity' , { num : '9.504166666666666' } , 'deg' ] ) ;
148+ expect ( evaluated . re ) . toBeCloseTo ( reparsedEvaluated . re ! , 10 ) ;
131149 } ) ;
132150
133151 test ( 'parse decimal degrees, serialize as DMS' , ( ) => {
134- // Use Quantity to preserve the degree unit (parsing 9.5° converts to radians)
135152 const expr = ce . box ( [ 'Quantity' , 9.5 , 'deg' ] ) ;
136153 const latex = expr . toLatex ( { dmsFormat : true } ) ;
137154 expect ( latex ) . toBe ( "9°30'" ) ;
138155 } ) ;
139156
140157 test ( 'parse DMS, serialize as decimal' , ( ) => {
141- const expr = ce . parse ( "9°30'" ) ;
142-
143- // Evaluate to get a single Quantity
144- const evaluated = expr . N ( ) ;
145- const latex = evaluated . toLatex ( { dmsFormat : false } ) ;
158+ // Use Quantity to preserve the degree unit for decimal serialization
159+ const expr = ce . box ( [ 'Quantity' , 9.5 , 'deg' ] ) ;
160+ const latex = expr . toLatex ( { dmsFormat : false } ) ;
146161
147- // Should use decimal degrees
162+ // Without dmsFormat, uses default unit serialization
148163 expect ( latex ) . toBe ( '9.5\\,\\mathrm{deg}' ) ;
149164 } ) ;
150165
151166 test ( 'normalization preserves mathematical value modulo period' , ( ) => {
152- // Use Quantity to preserve the degree unit
153167 const expr = ce . box ( [ 'Quantity' , 370 , 'deg' ] ) ;
154168 const normalized = expr . toLatex ( { angleNormalization : '0...360' } ) ;
155169 expect ( normalized ) . toBe ( '10°' ) ;
@@ -158,4 +172,12 @@ describe('Round-Trip Parsing and Serialization', () => {
158172 const diff = 370 - 10 ;
159173 expect ( diff ) . toBe ( 360 ) ;
160174 } ) ;
175+
176+ test ( 'negative DMS round-trip' , ( ) => {
177+ const input = "-9°30'" ;
178+ const expr = ce . parse ( input ) ;
179+
180+ // Should be approximately -9.5° in radians
181+ expect ( expr . N ( ) . re ) . toBeCloseTo ( - 9.5 * Math . PI / 180 , 10 ) ;
182+ } ) ;
161183} ) ;
0 commit comments