1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <comphelper/errcode.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxconv.hxx"
26 float ImpGetSingle( const SbxValues
* p
)
34 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
39 nRes
= p
->nChar
; break;
41 nRes
= p
->nByte
; break;
44 nRes
= p
->nInteger
; break;
47 nRes
= p
->nUShort
; break;
49 nRes
= static_cast<float>(p
->nLong
); break;
51 nRes
= static_cast<float>(p
->nULong
); break;
53 nRes
= p
->nSingle
; break;
55 case SbxBYREF
| SbxDECIMAL
:
56 if (!p
->pDecimal
|| !p
->pDecimal
->getSingle(nRes
))
66 if( p
->eType
== SbxCURRENCY
)
67 dVal
= ImpCurrencyToDouble( p
->nInt64
);
68 else if( p
->eType
== SbxSALINT64
)
69 dVal
= static_cast<float>(p
->nInt64
);
70 else if( p
->eType
== SbxSALUINT64
)
71 dVal
= static_cast<float>(p
->uInt64
);
75 if( dVal
> SbxMAXSNG
)
77 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
78 nRes
= static_cast< float >(SbxMAXSNG
);
80 else if( dVal
< SbxMINSNG
)
82 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
83 nRes
= static_cast< float >(SbxMINSNG
);
85 // tests for underflow - storing value too small for precision of single
86 else if( dVal
> 0 && dVal
< SbxMAXSNG2
)
88 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
89 nRes
= static_cast< float >(SbxMAXSNG2
);
91 else if( dVal
< 0 && dVal
> SbxMINSNG2
)
93 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
94 nRes
= static_cast< float >(SbxMINSNG2
);
97 nRes
= static_cast<float>(dVal
);
100 case SbxBYREF
| SbxSTRING
:
109 if( ImpScan( *p
->pOUString
, d
, t
, nullptr ) != ERRCODE_NONE
)
111 else if( d
> SbxMAXSNG
)
113 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
114 nRes
= static_cast< float >(SbxMAXSNG
);
116 else if( d
< SbxMINSNG
)
118 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
119 nRes
= static_cast< float >(SbxMINSNG
);
122 nRes
= static_cast<float>(d
);
127 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
129 nRes
= pVal
->GetSingle();
132 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
); nRes
= 0;
137 case SbxBYREF
| SbxCHAR
:
138 nRes
= *p
->pChar
; break;
139 case SbxBYREF
| SbxBYTE
:
140 nRes
= *p
->pByte
; break;
141 case SbxBYREF
| SbxINTEGER
:
142 case SbxBYREF
| SbxBOOL
:
143 nRes
= *p
->pInteger
; break;
144 case SbxBYREF
| SbxLONG
:
145 nRes
= static_cast<float>(*p
->pLong
); break;
146 case SbxBYREF
| SbxULONG
:
147 nRes
= static_cast<float>(*p
->pULong
); break;
148 case SbxBYREF
| SbxERROR
:
149 case SbxBYREF
| SbxUSHORT
:
150 nRes
= *p
->pUShort
; break;
151 case SbxBYREF
| SbxSINGLE
:
152 nRes
= *p
->pSingle
; break;
153 // from here had to be tested
154 case SbxBYREF
| SbxDATE
:
155 case SbxBYREF
| SbxDOUBLE
:
156 aTmp
.nDouble
= *p
->pDouble
; goto ref
;
157 case SbxBYREF
| SbxSALINT64
:
158 case SbxBYREF
| SbxCURRENCY
:
159 aTmp
.nInt64
= *p
->pnInt64
; goto ref
;
160 case SbxBYREF
| SbxSALUINT64
:
161 aTmp
.uInt64
= *p
->puInt64
; goto ref
;
163 aTmp
.eType
= SbxDataType( p
->eType
& 0x0FFF );
164 p
= &aTmp
; goto start
;
167 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
); nRes
= 0;
172 void ImpPutSingle( SbxValues
* p
, float n
)
179 aTmp
.pChar
= &p
->nChar
; goto direct
;
181 aTmp
.pByte
= &p
->nByte
; goto direct
;
184 aTmp
.pInteger
= &p
->nInteger
; goto direct
;
186 aTmp
.pLong
= &p
->nLong
; goto direct
;
188 aTmp
.pULong
= &p
->nULong
; goto direct
;
191 aTmp
.pUShort
= &p
->nUShort
; goto direct
;
194 aTmp
.pnInt64
= &p
->nInt64
; goto direct
;
196 aTmp
.puInt64
= &p
->uInt64
; goto direct
;
198 case SbxBYREF
| SbxDECIMAL
:
200 SbxDecimal
* pDec
= ImpCreateDecimal( p
);
201 if( !pDec
->setSingle( n
) )
202 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
);
206 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
207 p
= &aTmp
; goto start
;
209 // from here no tests
211 p
->nSingle
= n
; break;
214 p
->nDouble
= n
; break;
216 case SbxBYREF
| SbxSTRING
:
221 p
->pOUString
= new OUString
;
222 // tdf#107953 - show 9 significant digits
223 ImpCvtNum( static_cast<double>(n
), 9, *p
->pOUString
);
228 SbxValue
* pVal
= dynamic_cast<SbxValue
*>( p
->pObj
);
230 pVal
->PutSingle( n
);
232 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT
);
235 case SbxBYREF
| SbxCHAR
:
236 *p
->pChar
= ImpDoubleToChar(n
); break;
237 case SbxBYREF
| SbxBYTE
:
238 *p
->pByte
= ImpDoubleToByte(n
); break;
239 case SbxBYREF
| SbxINTEGER
:
240 case SbxBYREF
| SbxBOOL
:
241 *p
->pInteger
= ImpDoubleToInteger(n
); break;
242 case SbxBYREF
| SbxERROR
:
243 case SbxBYREF
| SbxUSHORT
:
244 *p
->pUShort
= ImpDoubleToUShort(n
); break;
245 case SbxBYREF
| SbxLONG
:
246 *p
->pLong
= ImpDoubleToLong(n
); break;
247 case SbxBYREF
| SbxULONG
:
248 *p
->pULong
= ImpDoubleToULong(n
); break;
249 case SbxBYREF
| SbxSINGLE
:
250 *p
->pSingle
= n
; break;
251 case SbxBYREF
| SbxDATE
:
252 case SbxBYREF
| SbxDOUBLE
:
253 *p
->pDouble
= static_cast<double>(n
); break;
254 case SbxBYREF
| SbxSALINT64
:
255 *p
->pnInt64
= ImpDoubleToSalInt64(n
); break;
256 case SbxBYREF
| SbxSALUINT64
:
257 *p
->puInt64
= ImpDoubleToSalUInt64(n
); break;
258 case SbxBYREF
| SbxCURRENCY
:
262 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); d
= SbxMAXCURR
;
264 else if( n
< SbxMINCURR
)
266 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW
); d
= SbxMINCURR
;
272 *p
->pnInt64
= ImpDoubleToCurrency( d
); break;
275 SbxBase::SetError( ERRCODE_BASIC_CONVERSION
);
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */